package com.yuncheng.spcyApi.busService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.yuncheng.constant.SysUserConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.spcyApi.utils.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.yuncheng.spcyApi.bo.SqsxXcjcQxParamBo;
import com.yuncheng.spcyApi.config.SpcyMinioUtils;
import com.yuncheng.spcyApi.constant.common.*;
import com.yuncheng.spcyApi.constant.flow.*;
import com.yuncheng.spcyApi.constant.jcy.JcydjbConstant;
import com.yuncheng.spcyApi.constant.other.BhgzConstant;
import com.yuncheng.spcyApi.constant.wjml.WjmlConstant;
import com.yuncheng.entity.*;
import com.yuncheng.exception.BusinessException;

import com.yuncheng.spcyApi.flow.SpcyFlowConstant;
import com.yuncheng.spcyApi.flowService.*;
import com.yuncheng.spcyApi.mapper.SpcySqsxYbmjlMapper;
import com.yuncheng.spcyApi.mapper.SpcySqsxYwxxMapper;
import com.yuncheng.spcyApi.mapper.XzXkMapper;
import com.yuncheng.spcyApi.selectXzxkApi.constant.XzXkConstant;
import com.yuncheng.spcyApi.selectXzxkApi.vo.*;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.spcyService.SpcyFlfgService;
import com.yuncheng.spcyApi.spcyService.SqsxYwxxInfoService;
import com.yuncheng.vo.*;
import com.yuncheng.spcyApi.vo.common.Result;
import com.yuncheng.spcyApi.vo.flow.AgentVo;
import com.yuncheng.spcyApi.vo.jcy.HcyXpVo;
import com.yuncheng.spcyApi.vo.jcy.RsHcyVo;
import com.yuncheng.spcyApi.vo.wjmb.SqsxWjmbVo;
import com.yuncheng.spcyApi.vo.sqsx.SqsxXpryNumVo;
import com.yuncheng.spcyApi.vo.sqsx.SqsxZzZyYbNumVo;
import com.yuncheng.spcyApi.vo.sqsx.YbmHcyListVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.ddr.poi.html.HtmlRenderPolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import utils.*;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 业务方法
 */
@Component(value = "busService")
public class BusService {
    private static final Logger log = LoggerFactory.getLogger(BusService.class);

    @Resource
    private ISpcySqsxBaiscService spcySqsxBaiscService;

    @Resource
    private ISpcySqsxJobService spcySqsxJobService;

    @Resource
    private IYcSysUserService ycSysUserService;

    @Resource
    private SmsUtils smsUtils;

    @Resource
    private SendMailUtils sendMailUtils;

    @Resource
    private ISpcySqsxXpjlService spcySqsxXpjlService;

    @Resource
    private ISpcyRsHcyService spcyRsHcyService;

    @Resource
    private ISpcyLogService spcyLogService;

    @Resource
    private SpcySqsxYbmjlMapper spcySqsxYbmjlMapper;

    @Resource
    private ISpcySqsxWjjlService spcySqsxWjjlService;

    @Resource
    private ISpcySqsxDxyxfsjlService spcySqsxDxyxfsjlService;

    @Resource
    private ISpcyQyJbxxService spcyQyJbxxService;

    @Resource
    private ISpcyRsGcyService spcyRsGcyService;

    @Resource
    private ISpcySqsxJcxmService spcySqsxJcxmService;

    @Resource
    private ISpcyRsWjmbService spcyRsWjmbService;

    @Resource
    private ISpcyRsBhgzService spcyRsBhgzService;

    @Resource
    @Lazy
    private YpscFlowService ypscFlowService;

    @Resource
    @Lazy
    private YpzcFlowService ypzcFlowService;

    @Resource
    @Lazy
    private YpltFlowService ypltFlowService;

    @Resource
    @Lazy
    private YlqxFlowService ylqxFlowService;

    @Resource
    @Lazy
    private HzpFlowService hzpFlowService;

    @Resource
    @Lazy
    private YpzclcFlowService ypzclcFlowService;

    @Resource
    @Lazy
    private FlowJobBlsxService flowJobBlsxService;

    @Resource
    @Lazy
    private ISpcySqsxBqbzjlService spcySqsxBqbzjlService;

    @Resource
    @Lazy
    private ISpcySqsxBczgjlService spcySqsxBczgjlService;

    @Resource
    @Lazy
    private SpcyFlfgService spcyFlfgService;

    @Resource
    @Lazy
    private ISpcySqsxXptjjlService spcySqsxXptjjlService;

    @Resource
    @Lazy
    private XzXkMapper xzXkMapper;

    @Resource
    private LocalFileUploadUtils localFileUploadUtils;

    @Resource
    @Lazy
    private SpcyMinioUtils spcyMinioUtils;

    @Resource
    private SpcySqsxYwxxMapper spcySqsxYwxxMapper;

    @Resource
    private SqsxYwxxInfoService sqsxYwxxInfoService;

    @Resource
    private ISpcySqsxRcjgService spcySqsxRcjgService;

    /** -------------------------------------------【 获取word模板替换关键字 - 功能 】--------------------------------------------------------- */

    /** 申请事项通用信息-将对象转为map */
    public Map<String, String> getObjectMap(String sqsxid, String xpid){
        return this.getObjectMap(sqsxid, null, xpid, true, true);
    }

    /** 申请事项通用信息-将对象转为map */
    public Map<String, String> getObjectMap(String sqsxid, SpcySqsxBasic sqsx, String xpid){
        return this.getObjectMap(sqsxid, sqsx, xpid, true, true);
    }

    public Map<String, String> getObjectMap(String sqsxid, String xpid, boolean isXpjl, boolean isGcy){
        return this.getObjectMap(sqsxid, null, xpid, isXpjl, isGcy);
    }

    /**
     * 申请事项通用信息-将对象转为map
     * @param sqsxid
     * @param xpid 选派记录id
     * @param isXpjl 选派记录
     * @param isGcy 观察员
     * @return
     */
    public Map<String, String> getObjectMap(String sqsxid, SpcySqsxBasic basic, String xpid, boolean isXpjl, boolean isGcy){
        Map<String,String> objectMap = new HashMap<>();
        if (basic == null) {
            basic = spcySqsxBaiscService.getById(sqsxid);
        }

        if (basic != null) {
            Map<String, String> map = ObjectCopyUtils.objectToMap(basic);
            objectMap.putAll(map);
        }

        if (StringUtils.isNotBlank(basic.getFcygcyid()) && isGcy){
            SpcyRsGcy gcy = spcyRsGcyService.getById(basic.getFcygcyid());
            Map<String, String> map1 = ObjectCopyUtils.objectToMap(gcy);
            objectMap.putAll(map1);
        }

        if (StringUtils.isNotBlank(xpid)) {
            SpcySqsxXpjl xpjl = spcySqsxXpjlService.getById(xpid);
            Map<String, String> map1 = ObjectCopyUtils.objectToMap(xpjl);
            objectMap.putAll(map1);
        }

        String nowUser = "xxxxx";// 当前用户
        String nowUserPhone = " "; // 当前用户电话
        String dwdh = " "; // 当前用户单位电话
        String userEmail = " ";
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser != null){
            nowUser = currentUser.getName();
            YcSysUser sysUser = ycSysUserService.getUserJbxxById(currentUser.getId());
            if (sysUser != null){
                if (StringUtils.isNotBlank(sysUser.getPhone())) {
                    nowUserPhone = sysUser.getPhone();
                }
                if (StringUtils.isNotBlank(sysUser.getFdwdh())) {
                    dwdh = sysUser.getFdwdh();
                }
                if (StringUtils.isNotBlank(sysUser.getEmail())){
                    userEmail = sysUser.getEmail();
                }
            }
        }

        /*String bqbzkssj = "xxxxx"; // 补齐补正开始时间
        String bqbzwcsj = "xxxxx"; // 补齐补正完成时间*/

        String sxjssj = "xxxxx"; // 接收时间
        String deadline = "xxxxx"; // 检查截止时间
        String spjzsj = "xxxxx"; // 审评截止时间
        String nowTime = "xxxxx"; // 当前时间
        String startTime = "xxxxx"; // 查验开始时间
        String endTime = "xxxxx"; // 查验结束时间
        String spkssj = "xxxxx"; // 审评开始时间
        String spjssj = "xxxxx"; // 审评结束时间

        String jcsj = "xxxxx"; // 检查时间-区间
        String jcts = "xxxxx"; // 检查天数
        String spjcsj = "xxxxx"; // 审评时间-区间

        String leader = " "; // 检查人员组长
        String members = " "; // 检查人员组员
        String jcry = " "; // 多个 检查人员
        String faleader = " "; // 方案检查组长
        String famembers = " "; // 方案检查组员

        String spleader = " "; // 审评人员组长
        String spmembers = " "; // 审评人员组员
        String spjcry = " "; // 多个 审评人员

        String zz = " "; // 组长
        String zy = " "; // 组员
        String ybjcy = " "; // 预备

        nowTime = DateConversionUtils.DateToYMDString(DateConversionUtils.DateToString(new Date())); // 当前时间

        // 事项接收时间
        if (basic.getFslsj() != null){
            sxjssj = DateConversionUtils.DateToStringNew(basic.getFslsj());
        }

        // 查验时间
        if (basic.getFcykssj() != null) {
            startTime = DateConversionUtils.DateToStringNew(basic.getFcykssj());
            deadline = DateConversionUtils.DateToStringNew(DateUtil.offsetDay(basic.getFcykssj(),-1));
        }
        if (basic.getFcyjssj() != null) {
            endTime = DateConversionUtils.DateToStringNew(basic.getFcyjssj());
        }
        if (basic.getFcykssj() != null){
            jcsj = startTime + "-" + endTime;
        }

        if (basic.getFcykssj() != null && basic.getFcyjssj() != null){
            long betweenDay = DateUtil.between(basic.getFcykssj(), basic.getFcyjssj(), DateUnit.DAY);
            int betDay = new Long(betweenDay).intValue() + 1;
            jcts = String.valueOf(betDay);
        }

        // 审评时间
        /*if (basic.getFspkssj() != null) {
            spkssj = DateConversionUtils.DateToYMDString(DateConversionUtils.DateToString(basic.getFspkssj()));
            spjzsj = DateConversionUtils.DateToYMDString(DateConversionUtils.DateToString(DateUtil.offsetDay(basic.getFspkssj(),-1)));
        }
        if (basic.getFspjssj() != null) {
            spjssj = DateConversionUtils.DateToYMDString(DateConversionUtils.DateToString(basic.getFspjssj()));
        }
        if (basic.getFspkssj() != null){
            spjcsj = spkssj + "-" + spjssj;
        }*/

        List<SpcySqsxXpjl> dispatchList = new ArrayList<>();

        // 审评、选派记录,同意
//        List<SpcySqsxXpjl> spDispatchList = new ArrayList<>();

        // 查验、选派记录,同意
        if (isXpjl) {
            dispatchList = spcySqsxXpjlService.getTyXpXpjlListNotOrder(sqsxid);

            /*spDispatchList = spcySqsxXpjlService.list(new LambdaQueryWrapper<SpcySqsxXpjl>()
                    .eq(SpcySqsxXpjl::getFsqsxid, sqsxid)
                    .eq(SpcySqsxXpjl::getFspcylx, SpcyConstant.FLOW_TYPE_SP)
                    .eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_YES));*/
        }

        // 检查人员、选派人员不为空
        if (CollectionUtils.isNotEmpty(dispatchList)){
            for (SpcySqsxXpjl dispatch : dispatchList){
                // 组长
                if (StringUtils.isNotBlank(dispatch.getFssk()) && dispatch.getFssk().contains(SpcyConstant.HCY_SSK_ZZ)) {

                    // 拼接检查员工作单位和电话
                    leader += dispatch.getFjcyxm() + "(" + dispatch.getFgzdw()+" "+dispatch.getFjcylxdh()+")";

                    // 组长人员
                    if (StringUtils.isBlank(zz)) {
                        zz += dispatch.getFjcyxm();
                    }else {
                        zz += "、"+dispatch.getFjcyxm();
                    }

                    // 拼接检查员专业
                    StringBuffer str = new StringBuffer("");
                    if (StringUtils.isBlank(faleader)){
                        if (StringUtils.isNotBlank(dispatch.getFsczy())) {
                            str.append(dispatch.getFjcyxm()).append("(熟悉").append(dispatch.getFsczy()).append(")");
                        }else {
                            str.append(dispatch.getFjcyxm()).append("(").append(")");
                        }
                    }
                    if (StringUtils.isNotBlank(faleader)){
                        if (StringUtils.isNotBlank(dispatch.getFsczy())) {
                            str.append("、").append(dispatch.getFjcyxm()).append("(熟悉").append(dispatch.getFsczy()).append(")");
                        }else {
                            str.append("、").append(dispatch.getFjcyxm()).append("(").append(")");
                        }
                    }

                    faleader += str.toString();
                }

                // 组员
                if (StringUtils.isNotBlank(dispatch.getFssk()) && dispatch.getFssk().contains(SpcyConstant.HCY_SSK_ZY)){

                    // 拼接检查员工作单位和电话
                    members += dispatch.getFjcyxm() + "(" + dispatch.getFgzdw() + " "+dispatch.getFjcylxdh()+")";

                    // 组员人员
                    if (StringUtils.isBlank(zy)) {
                        zy += dispatch.getFjcyxm();
                    }else {
                        zy += "、"+dispatch.getFjcyxm();
                    }

                    // 拼接检查员专业
                    StringBuffer str = new StringBuffer("");
                    if (StringUtils.isBlank(famembers)){
                        if (StringUtils.isNotBlank(dispatch.getFsczy())) {
                            str.append(dispatch.getFjcyxm()).append("(熟悉").append(dispatch.getFsczy()).append(")");
                        }else {
                            str.append(dispatch.getFjcyxm()).append("(").append(")");
                        }
                    }
                    if (StringUtils.isNotBlank(famembers)){
                        if (StringUtils.isNotBlank(dispatch.getFsczy())) {
                            str.append("、").append(dispatch.getFjcyxm()).append("(熟悉").append(dispatch.getFsczy()).append(")");
                        }else {
                            str.append("、").append(dispatch.getFjcyxm()).append("(").append(")");
                        }
                    }

                    famembers += str.toString();
                }

                // 预备检查员
                if (StringUtils.isNotBlank(dispatch.getFssk()) && dispatch.getFssk().contains(SpcyConstant.HCY_SSK_YB)){

                    // 预备检查人员
                    if (StringUtils.isBlank(ybjcy)) {
                        ybjcy += dispatch.getFjcyxm() + "("+SpcyConstant.HCY_SSK_YB+")";
                    }else {
                        ybjcy += "、"+dispatch.getFjcyxm() + "("+SpcyConstant.HCY_SSK_YB+")";
                    }

                }

                // 多个检查人员拼接
                if (StringUtils.isBlank(jcry)) {
                    jcry += dispatch.getFjcyxm();
                }else {
                    jcry += "、"+dispatch.getFjcyxm();
                }
            }
        }

        /*if (CollectionUtils.isNotEmpty(spDispatchList)){
            for (SpcySqsxXpjl dispatch : spDispatchList){
                if (StringUtils.isNotBlank(dispatch.getFssk()) && dispatch.getFssk().contains(SpcyConstant.HCY_SSK_ZZ)) { // 组长
                    spleader += dispatch.getFjcyxm() + "(" + dispatch.getFgzdw()+" "+dispatch.getFjcylxdh()+")";
                } else {
                    spmembers += dispatch.getFjcyxm() + "(" + dispatch.getFgzdw() + " "+dispatch.getFjcylxdh()+")";
                }
                spjcry += dispatch.getFjcyxm() + ",";
            }
        }*/

//        String fxqxqk = " "; // 发现缺陷情况
        if (basic != null && StringUtils.isNotBlank(basic.getFywflcode())) {
            // 药品生产
            if (basic.getFywflcode().equals(SpcyConstant.YWFL_CODE_YPSC)) {
//                Map ypscJcxmXX = spcySqsxJcxmService.getJcxmXXInYpsc(sqsxid);
//                fxqxqk = spcySqsxJcxmService.getQxqkByJcxmXxMap(ypscJcxmXX);

            }else if (basic.getFywflcode().equals(SpcyConstant.YWFL_CODE_YPJY)){
                // 药品流通
//                objectMap.putAll(this.mbFieldCommonByYplt(sqsxid, basic));

            }else if (basic.getFywflcode().equals(SpcyConstant.YWFL_CODE_HZP)){
                // 化妆品
//                objectMap.putAll(this.mbFieldCommonByHzp(sqsxid, basic));

            }else if (basic.getFywflcode().equals(SpcyConstant.YWFL_CODE_YLQX)){

            }else if (basic.getFywflcode().equals(SpcyConstant.YWFL_CODE_YPZC)){

            }else if (basic.getFywflcode().equals(SpcyConstant.YWFL_CODE_YPZCLC)){

            }
        }
//        objectMap.put(SpcyConstant.YWZDXX_FXQXQK_REPLACE,fxqxqk); // 发现缺陷情况

        /** 企业关键人员相关信息 */
//        objectMap.putAll(this.genZwfwQyGjryxx(basic, sqsxid));

        objectMap.put("${data.nowUser}",nowUser);
        objectMap.put("${data.nowUserPhone}", nowUserPhone);
        objectMap.put("${data.lxdh}", nowUserPhone);
        objectMap.put("${data.dwdh}", dwdh);
        objectMap.put("${data.userEmail}", userEmail);
        objectMap.put("${data.deadline}",deadline);
        objectMap.put("${data.spjzsj}",spjzsj);
        objectMap.put("${data.nowTime}",nowTime);
        objectMap.put("${data.sxjssj}",sxjssj);
        objectMap.put("${data.kssj}",startTime);
        objectMap.put("${data.jssj}",endTime);
        objectMap.put("${data.spkssj}",spkssj);
        objectMap.put("${data.spjssj}",spjssj);
        objectMap.put("${data.jcsj}",jcsj);
        objectMap.put("${data.spjcsj}",spjcsj);
        objectMap.put("${data.leader}",leader);
        objectMap.put("${data.faleader}",faleader);
        objectMap.put("${data.members}",members);
        objectMap.put("${data.famembers}",famembers);
        objectMap.put("${data.jcry}",jcry);
        objectMap.put("${data.jcts}",jcts);
        objectMap.put("${data.spleader}",spleader);
        objectMap.put("${data.spmembers}",spmembers);
        objectMap.put("${data.spjcry}",spjcry);
        objectMap.put("${data.zz}",zz);
        objectMap.put("${data.zy}",zy);
        objectMap.put("${data.ybjcy}",ybjcy);

        /*objectMap.put("${data.bqbzkssj}",bqbzkssj);
        objectMap.put("${data.bqbzwcsj}",bqbzwcsj);*/
        return objectMap;
    }

    // 医疗器械-固定模板
    public Map<String, String> mbFieldByYlqx(){
        Map<String,String> objectMap = new HashMap<>();

        // 缺陷条款-模板
        StringBuffer qxtkmb = new StringBuffer();
        qxtkmb.append("现场检查发现缺陷共X项，其中关键项目X项，一般项目X项：\n" +
                "1、规范第XX条：XXXXX（指导原则*X.X.X）；\n" +
                "2、附录X.X.X：XXXX（指导原则X.X.X）\n" +
                "如\n" +
                "1、规范 第五条  123123123 (指导原则1.1.1) \n" +
                "2、规范 第五条  12312312 (指导原则1.1.3) \n" +
                "3、规范 第六条  312312 (指导原则1.2.1) \n" +
                "4、规范 第五十条  1231231321 (指导原则7.6.2) \n" +
                "严重缺陷: 0项。");

        objectMap.put("${data.qxtkmb}",qxtkmb.toString());

        return objectMap;
    }

    // 药品流通 -检查项目 - 固定模板
    public Map<String, String> mbFieldByYplt(String sqsxid){
        Map<String,String> objectMap = new HashMap<>();

        Map jcxmXXInYplt = spcySqsxJcxmService.getJcxmXXInYplt(sqsxid);

        Integer ybqxSize = Integer.valueOf(jcxmXXInYplt.get("ybqxSize").toString());
        Integer zyqxSize = Integer.valueOf(jcxmXXInYplt.get("zyqxSize").toString());
        Integer yzqxSize =Integer.valueOf(jcxmXXInYplt.get("yzqxSize").toString());

        int proTotal = ybqxSize+zyqxSize+yzqxSize; // 实际检查量

        // 技术审查结论-意见
        StringBuffer scyj = new StringBuffer();
        String zxfcyybqx = jcxmXXInYplt.get("zxfcyybqx").toString();
        if (StringUtils.isNotBlank(zxfcyybqx)) {
            scyj.append(zxfcyybqx);
        }
        String zxfcyzyqx = jcxmXXInYplt.get("zxfcyzyqx").toString();
        if (StringUtils.isNotBlank(zxfcyzyqx)) {
            scyj.append(zxfcyzyqx);
        }
        String zxfcyyzqx = jcxmXXInYplt.get("zxfcyyzqx").toString();
        if (StringUtils.isNotBlank(zxfcyyzqx)) {
            scyj.append(zxfcyyzqx);
        }

        objectMap.put("${data.total}", jcxmXXInYplt.get("total").toString()); // 总检查项目
        objectMap.put("${data.proTotal}", String.valueOf(proTotal)); // 实际检查项目

        objectMap.put("${data.ybqxSize}", String.valueOf(ybqxSize));
        objectMap.put("${data.zyqxSize}", String.valueOf(zyqxSize));
        objectMap.put("${data.yzqxSize}", String.valueOf(yzqxSize));

        objectMap.put("${data.zxybqxSize}", jcxmXXInYplt.get("zxybqxSize").toString());
        objectMap.put("${data.zxzyqxSize}", jcxmXXInYplt.get("zxzyqxSize").toString());
        objectMap.put("${data.zxyzqxSize}", jcxmXXInYplt.get("zxyzqxSize").toString());

        objectMap.put("${data.scyj}", scyj.toString()); // 审查意见结论
        return objectMap;
    }

    /**
     * 药品流通 - 情况汇总评估 - 字段模板
     * @param sqsxid 申请事项id
     * @return
     */
    public Map<String, String> mbFieldQkhzByYplt(String sqsxid){
        // 事项名称
        String fsxmc = "";
        SpcySqsxBasic basic = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (basic != null && StringUtils.isNotBlank(basic.getFsxmc())){
            fsxmc = basic.getFsxmc();
        }

        Map<String,String> resultMap = new HashMap<>();
        String qkhzpgInYplt = spcySqsxJcxmService.getQkhzpgInYplt(sqsxid, fsxmc);

        resultMap.put("${data.qkhzpg}",qkhzpgInYplt);
        return resultMap;
    }

    /**
     * 药品流通 - 通用模板字段
     */
    public Map<String, String> mbFieldCommonByYplt(String sqsxid, SpcySqsxBasic sqsx){
        if (sqsx == null){
            sqsx = spcySqsxBaiscService.getById(sqsxid);
        }

        sqsxid = sqsx.getFsxsqjbxxid();

        Map<String,String> resultMap = new HashMap<>();

        String jyfsField = "${data.jyfs}"; // 经营方式
        /**
         * 经营范围 - 获取经营方式(jyfs)
         */
        List<XzspJyfwVo> jyfwList = xzXkMapper.selectJyfw(sqsxid);
        if (CollectionUtils.isNotEmpty(jyfwList)){
            if (StringUtils.isNotBlank((String)jyfwList.get(0).getJyfs2())){
                resultMap.put(jyfsField, jyfwList.get(0).getJyfs2());
            }else {
                resultMap.put(jyfsField, "xxxxx");
            }
        }else {
            resultMap.put(jyfsField, "xxxxx");
        }


        String ckdz = "xxx"; // 仓库地址
        String ckmc = "xxx"; // 仓库名称
        // 仓库信息
        List<XzspCkVo> ckList = xzXkMapper.ypltSelectCkxx(sqsxid);
        if (CollectionUtils.isNotEmpty(ckList)){
            for (XzspCkVo ck : ckList){
                if (ck.getCkmc() != null){
                    ckmc = ck.getCkmc();
                }
                if (ck.getXxdz() != null){
                    ckdz = ck.getXxdz();
                }
            }
        }

        resultMap.put("${data.ckdz}",ckdz);
        resultMap.put("${data.ckmc}",ckmc);

        return resultMap;
    }

    /**
     * 化妆品 - 通用模板字段
     */
    public Map<String, String> mbFieldCommonByHzp(String sqsxid){
        Map<String,String> resultMap = new HashMap<>();

        String defaultParams = "xxxxx"; // 默认参数

        String fdbdrmc = defaultParams; // 法定代表人姓名
        String scfzrmc = defaultParams; // 生产负责人姓名
        String zlaqfzrmc = defaultParams; // 企业质量管理人姓名
        String fdbdrlxdh = defaultParams; // 法定代表人联系电话

        // 企业信息
        /*List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyList)){

        }*/

        // 企业人员
        List<QyRyVo> qyryList = xzXkMapper.selectQyRyList(sqsxid, null, true);
        if (CollectionUtils.isNotEmpty(qyryList)){
            for (QyRyVo ry : qyryList){
                if (ry.getRyfl() != null && ry.getRyfl().equals(XzXkConstant.RYFL_FDDBR)){
                    fdbdrmc = ry.getXl();
                    if (ry.getSj() != null) {
                        fdbdrlxdh = ry.getSj();
                    }
                }else if (ry.getRyfl() != null && ry.getRyfl().equals(XzXkConstant.RYFL_SCFZR)){
                    scfzrmc = ry.getXm();
                }else if (ry.getRyfl() != null && ry.getRyfl().equals(XzXkConstant.RYFL_QYZLFZR)){
                    zlaqfzrmc = ry.getXm();
                }
            }
        }

        resultMap.put("${data.fdbdrmc}",fdbdrmc);
        resultMap.put("${data.fdbdrlxdh}",fdbdrlxdh);
        resultMap.put("${data.scfzrmc}",scfzrmc);
        resultMap.put("${data.zlaqfzrmc}",zlaqfzrmc);

        String zdmj = defaultParams; // 占地面积
        String jzzmj = defaultParams; // 建筑总面积
        String bgmj = defaultParams; // 办公面积
        String cjmj = defaultParams; // 经营/厂房/车间面积
        String fzyfmj = defaultParams; // 辅助用房面积
        String ccmj = defaultParams; // 仓储面积
        String jhmj = defaultParams; // 清洁面积
        String jymj = defaultParams; // 检验面积
        String zjsmj = defaultParams; // 制剂室面积
        String sysmj = defaultParams; // 实验室面积

        // 经营场所
        List<XzspJycsVo> jycsList = xzXkMapper.selectJycs(sqsxid);
        if (CollectionUtils.isNotEmpty(jycsList)){
            XzspJycsVo map = jycsList.get(0);
            if (map.getZdmj() != null){
                zdmj = map.getZdmj().toString();
            }
            if (map.getJzzmj() != null){
                jzzmj = map.getJzzmj().toString();
            }
            if (map.getBgmj() != null){
                bgmj = map.getBgmj().toString();
            }
            if (map.getJycfmj() != null){
                cjmj = map.getJycfmj().toString();
            }
            if (map.getFzyfmj() != null){
                fzyfmj = map.getFzyfmj().toString();
            }
            if (map.getCcmj() != null){
                ccmj = map.getCcmj().toString();
            }
            if (map.getJhmj() != null){
                jhmj = map.getJhmj().toString();
            }
            if (map.getJymj() != null){
                jymj = map.getJymj().toString();
            }
            if (map.getZjsmj() != null){
                zjsmj = map.getZjsmj().toString();
            }
            if (map.getSysmj() != null){
                sysmj = map.getSysmj().toString();
            }
        }

        resultMap.put("${data.zdmj}",zdmj);
        resultMap.put("${data.jzzmj}",jzzmj);
        resultMap.put("${data.bgmj}",bgmj);
        resultMap.put("${data.cjmj}",cjmj);
        resultMap.put("${data.zdfzyfmjmj}",fzyfmj);
        resultMap.put("${data.ccmj}",ccmj);
        resultMap.put("${data.jhmj}",jhmj);
        resultMap.put("${data.jymj}",jymj);
        resultMap.put("${data.zjsmj}",zjsmj);
        resultMap.put("${data.sysmj}",sysmj);

        return resultMap;
    }

    /**
     * 化妆品 - 现场检查笔录-字段模板
     */
    public Map<String, String> mdFieldXcjcblByHzp(String sqsxid){
        Map<String,String> resultMap = new HashMap<>();

        Map jcxmXXInHzp = spcySqsxJcxmService.getJcxmXXInHzp(sqsxid);

        StringBuffer qxqk = new StringBuffer();
        String fcyyzqx = jcxmXXInHzp.get("fcyyzqx").toString();
        if (StringUtils.isNotBlank(fcyyzqx)) {
            qxqk.append(fcyyzqx);
        }
        String fcyzyqx = jcxmXXInHzp.get("fcyzyqx").toString();
        if (StringUtils.isNotBlank(fcyzyqx)) {
            qxqk.append(fcyzyqx);
        }
        String fcyybqx = jcxmXXInHzp.get("fcyybqx").toString();
        if (StringUtils.isNotBlank(fcyybqx)) {
            qxqk.append(fcyybqx);
        }

        resultMap.put("${data.xcjcqxqk}", qxqk.toString()); // 现场检查-缺陷情况
        resultMap.put("${data.ybqxSize}", jcxmXXInHzp.get("ybqxSize").toString());
        resultMap.put("${data.zyqxSize}", jcxmXXInHzp.get("zyqxSize").toString());
        resultMap.put("${data.yzqxSize}", jcxmXXInHzp.get("yzqxSize").toString());

        resultMap.put("${data.gjxmbfhsl}", jcxmXXInHzp.get("gjxmbfhsl").toString());
        resultMap.put("${data.gjxmxcsl}", jcxmXXInHzp.get("gjxmxcsl").toString());

        resultMap.put("${data.zdxmbfhsl}", jcxmXXInHzp.get("zdxmbfhsl").toString());
        resultMap.put("${data.zdxmxcsl}", jcxmXXInHzp.get("zdxmxcsl").toString());

        resultMap.put("${data.ybxmbfhsl}", jcxmXXInHzp.get("ybxmbfhsl").toString());
        resultMap.put("${data.ybxmxcsl}", jcxmXXInHzp.get("ybxmxcsl").toString());

        resultMap.put("${data.qxtotal}", jcxmXXInHzp.get("qxtotal").toString());
        resultMap.put("${data.zdqxtotal}", jcxmXXInHzp.get("zdqxtotal").toString());
        return resultMap;
    }

    /**
     * 构建同步政务系统-企业所在地区
     * @param flag 0: fszdq/fjcdz都赋值，1：fszdq赋值, 2: fjcdz赋值
     * @return SpcySqsxBasic
     */
    public SpcySqsxBasic genZwfwQySzdq(SpcySqsxBasic sqsx, int flag){
        if (StringUtils.isBlank(sqsx.getFsxsqjbxxid())){
            return sqsx;
        }

        QySzdqVo qySzdqVo = xzXkMapper.selectSqsxQySzdq(sqsx.getFsxsqjbxxid());
        if (qySzdqVo == null){
            return sqsx;
        }

        String szdq = "";
        if (StringUtils.isNotBlank(qySzdqVo.getSzds())){
            szdq += qySzdqVo.getSzds();
        }
        if (StringUtils.isNotBlank(qySzdqVo.getSzqx())){
            szdq += qySzdqVo.getSzqx();
        }
        if (StringUtils.isNotBlank(qySzdqVo.getSzxz())){
            szdq += qySzdqVo.getSzxz();
        }

        if (StringUtils.isBlank(szdq)){
            szdq = "/";
        }

        if (flag == 0) {
            sqsx.setFszdq(szdq);
            sqsx.setFjcdz(szdq);

        }else if (flag == 1){
            sqsx.setFszdq(szdq);

        }else if (flag == 2){
            sqsx.setFjcdz(szdq);
        }

        return sqsx;
    }

    /**
     * 获取企业关键人员模板关键字
     * */
    public Map<String, String> genZwfwQyGjryxx(SpcySqsxBasic sqsx, String sqsxid){
        Map<String,String> resultMap = new HashMap<>();
        if (sqsx == null){
            sqsx = spcySqsxBaiscService.getById(sqsxid);
        }

        sqsxid = sqsx.getFsxsqjbxxid();

        String defaultParams = "xxx"; // 默认参数
        String fdbdrmc = defaultParams; // 法定代表人姓名
        String scfzrmc = defaultParams; // 生产负责人姓名
        String zlglbmfzrmc = defaultParams; // 质量管理部门负责人姓名
        String zlaqfzrmc = defaultParams; // 企业质量管理人姓名
        String zlsqrmc = defaultParams; // 质量受权人姓名
        String qyfzrmc = defaultParams; // 企业负责人姓名
        String fdbdrlxdh = defaultParams; // 法定代表人联系电话

        // 企业人员
        List<QyRyVo> qyryList = null;
        try {
            qyryList = xzXkMapper.selectQyGjRyJbxxList(sqsxid, null, true);
        }catch (Exception e){
            log.error("获取企业关键人员模板关键字-错误：{}", e.getMessage());
        }
        if (CollectionUtils.isNotEmpty(qyryList)){
            for (QyRyVo ry : qyryList){
                if (StringUtils.isBlank(ry.getFzrlx())){
                    continue;
                }

                if (ry.getFzrlx().equals(XzXkConstant.RYFL_FDDBR_CODE)){
                    fdbdrmc = ry.getXm();
                    if (ry.getSj() != null) {
                        fdbdrlxdh = ry.getSj();
                    }
                }else if (ry.getFzrlx().equals(XzXkConstant.RYFL_SCFZR_CODE)){
                    scfzrmc = ry.getXm();
                }else if (ry.getFzrlx().equals(XzXkConstant.RYFL_QYZLFZR_CODE)){
                    zlaqfzrmc = ry.getXm();
                }else if (ry.getFzrlx().equals(XzXkConstant.RYFL_ZLGLBMFZR_CODE)){
                    zlglbmfzrmc = ry.getXm();
                }else if (ry.getFzrlx().equals(XzXkConstant.RYFL_ZLSQR_CODE)){
                    zlsqrmc = ry.getXm();
                }else if (ry.getFzrlx().equals(XzXkConstant.RYFL_QYFZR_CODE)){
                    qyfzrmc = ry.getXm();
                }
            }
        }

        resultMap.put("${data.fdbdrmc}",fdbdrmc);
        resultMap.put("${data.fdbdrlxdh}",fdbdrlxdh);
        resultMap.put("${data.scfzrmc}",scfzrmc);
        resultMap.put("${data.zlaqfzrmc}",zlaqfzrmc);
        resultMap.put("${data.zlglbmfzrmc}",zlglbmfzrmc);
        resultMap.put("${data.zlsqrmc}", zlsqrmc);
        resultMap.put("${data.qyfzrmc}", qyfzrmc);

        return resultMap;
    }

    /** -------------------------------------------【 模板初始化功能 - 功能 】--------------------------------------------------------- */

    /**
     * 业务分类编码替换
     * @param fywflcode
     * @return
     */
    public String replaceYwflcode(String fywflcode){
        String code = "";

        if (StringUtils.isNotBlank(fywflcode)){
            if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPZCLC)){
                code = SpcyConstant.YWFL_CODE_YPZC;
            }else {
                code = fywflcode;
            }
        }

        return code;
    }

    /**
     * 申请事项-对套用模板的文档进行重新选择模板-生成文件记录
     * @param sqsxid 申请事项id
     * @param fileid 附件记录id
     * @param wjmbid 模板id
     */
    public Result sqsxWjjlReloadWjmb(String sqsxid, String fileid, String wjmbid){
        if (StringUtils.isBlank(sqsxid)){
            return Result.error("事项参数为空！");
        }

        if (StringUtils.isBlank(wjmbid)){
            return Result.error("模板参数为空！");
        }

        SpcyRsWjmb wjmb = spcyRsWjmbService.getById(wjmbid);
        if (wjmb == null){
            return Result.error("操作失败，没有相匹配的模板数据！");
        }

        if (StringUtils.isBlank(wjmb.getFcflj())){
            return Result.error("操作失败，模板没有相关附件！");
        }

        SpcySqsxWjjl wjjl = null;
        if (StringUtils.isNotBlank(fileid)) {
            wjjl = spcySqsxWjjlService.getById(fileid);
            if (wjjl == null) {
                return Result.error("操作失败，没有相匹配的文件数据！");
            }
        }else {
            List<SpcySqsxWjjl> list = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                    .eq(SpcySqsxWjjl::getFpid, sqsxid)
                    .eq(SpcySqsxWjjl::getFwjlx1, wjmb.getFmblx1()));
            if (CollectionUtil.isNotEmpty(list) && list.size() == 1){
                wjjl = list.get(0);
                fileid = list.get(0).getId();
            }else {
                if (StringUtils.isBlank(fileid)){
                    return Result.error("文件参数为空！");
                }
            }
        }

        /** 附件业务相关内容 */
        String fbusid = ""; // 业务id
        String fbusmc = ""; // 业务名称
        String mblx = "";
        if (wjjl != null){
            mblx = wjjl.getFwjlx1();
            fbusid = wjjl.getFbusid();
            fbusmc = wjjl.getFbusmc();
        }

        /** 获取文件模板绝对路径 */
        String wjmbUrl = "";
        if (localFileUploadUtils.isEnableMinioWjmbFile()){
            File file = localFileUploadUtils.convertMinioToFile(localFileUploadUtils.getWjmbRelativeUrl(wjmb.getFcflj()), wjmb.getFwjlx());
            if (file != null) {
                wjmbUrl = file.getAbsolutePath();
            }
        }else {
            wjmbUrl = localFileUploadUtils.getWjmbAbsUrl(wjmb.getFcflj());
        }

        /** 文件模板doc转docx */
        if (wjmb.getFwjlx().equalsIgnoreCase(".doc")){
            String outUrl = localFileUploadUtils.getTempFileRootUrl() + "/" + GetUuIdUtils.NotReplaceUuId() + ".docx";
            try {
                WordPdfUtils.wordToDocx(wjmbUrl, outUrl);
                wjmbUrl = outUrl;
                wjmb.setFwjlx(".docx");
            }catch (Exception e){
                log.error("doc文档转docx文档错误：{}", e.getMessage());
            }
        }

        /** 重新生成附件及记录
         * // 选择文件，生成相应的函
         * */
        Result result = this.initWjmbWjjl(sqsxid, wjmbUrl, mblx, fbusid, fbusmc, fileid, wjjl, wjmbid);
        if (!result.isSuccess()){
            return Result.error(result.getMessage());
        }

        /** 删除旧的记录和附件 */
        if (wjjl != null) {
            spcySqsxWjjlService.removeById(fileid);
            if (StringUtils.isNotBlank(wjjl.getFcflj())) {
                localFileUploadUtils.deleteLocalFileUrl(wjjl.getFcflj());
            }
            if (StringUtils.isNotBlank(wjjl.getFjdcflj())) {
                localFileUploadUtils.deleteLocalFileUrl(wjjl.getFjdcflj());
            }
        }

        return Result.ok();
    }

    /**
     * 初始化文件记录 （选择模板功能）
     * @param sqsxid
     * @param wjmbUrl
     * @param type
     * @return
     */
    public Result initWjmbWjjl(String sqsxid, String wjmbUrl, String type, String fbusid, String fbusmc, String fileid, String wjmbid){
        return initWjmbWjjl(sqsxid, wjmbUrl, type, fbusid, fbusmc, fileid, null, wjmbid);
    }
    public Result initWjmbWjjl(String sqsxid, String wjmbUrl, String type, String fbusid, String fbusmc, String fileid, SpcySqsxWjjl oldWjjl,String wjmbid){
        Map<String,String> map = new HashMap<>();

        SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(sqsxid);

        boolean isWordContent = false;
        if (type.equals(WjmlConstant.xpxcscydh)) { // 选派现场审查员的函
            map = getObjectMap(sqsxid, sqsx, fbusid);
            isWordContent = true;

        }else if (type.equals(WjmlConstant.xpxcgcydh)) { // 选派现场观察员的函
            map = getObjectMap(sqsxid, sqsx, "");
            isWordContent = true;

        }else if (type.equals(WjmlConstant.xzjch)) { // 协助检查函
            map = getObjectMap(sqsxid, sqsx, "");
            isWordContent = true;

        }else if (type.equals(WjmlConstant.gcysm)) { // 观察员声明
            map = getObjectMap(sqsxid, sqsx, "");
            isWordContent = true;

        }else if (type.equals(WjmlConstant.qydwjctz)) { // 企业单位检查通知
            map = getObjectMap(sqsxid, sqsx, "");
            isWordContent = true;

        }else if (type.equals(WjmlConstant.jcfa)) { // 检查方案
            map = getObjectMap(sqsxid, sqsx, "");

        }else if (type.equals(WjmlConstant.hstz)){ // 函审通知
            map = getObjectMap(sqsxid, sqsx, fbusid);
            isWordContent = true;

        }else if (type.equals(WjmlConstant.ypzc_fhdwtz)){ // 复核单位通知
            map = getObjectMap(sqsxid, sqsx,"");
            isWordContent = true;

        }else if (type.equals(WjmlConstant.ypzc_zcctz)){ // 注册处通知
            map = getObjectMap(sqsxid, sqsx,"");
            isWordContent = true;

        }else if (type.equals(WjmlConstant.qytzs)){ // 企业通知书
            map = getObjectMap(sqsxid, sqsx, "");
            isWordContent = true;

        } else if (type.equals(WjmlConstant.xcjcbg)){

            // 构建检查项目-缺陷信息
            // 同步检查项目信息
            SqsxXcjcQxParamBo jcxmQxxx = spcyFlfgService.getJcxmQxxx(sqsxid, false);
            spcySqsxBaiscService.updateSqsxQxxx(sqsxid, jcxmQxxx);
            map = getObjectMap(sqsxid, sqsx, "");

            /** 获取现场章节描述-通过申请事项id */
            /*try {
                map.putAll(spcyFlfgService.genXcjczjStructureContent(sqsxid));
            }catch (Exception e){
                log.error("获取文件模板章节描述-通过申请事项id: {}", e.getMessage());
            }*/
        }else {
            map = getObjectMap(sqsxid,sqsx, "");
        }

        String nowNodeName = type;
        String fywflcode = "";
        if (sqsx == null) {
            fywflcode = this.getYwflCode(sqsxid);
        }else {
            fywflcode = sqsx.getFywflcode();
        }


        if (StringUtils.isNotBlank(fywflcode)){
            // 药品流通 模板字段
            if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPJY)){
                /*if (CollectionUtil.newArrayList(
                        WjmlConstant.xcjcbg,
                        WjmlConstant.jsscbg,
                        WjmlConstant.xcjcshj,
                        WjmlConstant.yshmb,
                        WjmlConstant.zhpdbgs,
                        WjmlConstant.xcjcgg
                ).contains(type)){
                    map.putAll(this.mbFieldByYplt(sqsxid));
                }*/
            }
            else if (fywflcode.equals(SpcyConstant.YWFL_CODE_HZP)){
                if (CollectionUtil.newArrayList(
                        WjmlConstant.xcjcbl,
                        WjmlConstant.zgtzs,
                        WjmlConstant.yshmb,
                        WjmlConstant.hzp_xcjcjl
                ).contains(type)){
                    this.mdFieldXcjcblByHzp(sqsxid);
                }
            }
        }

        String czrid = ""; // 操作人id
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser != null){
            czrid = currentUser.getId();
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        String wjxsmc = "";
        if (StringUtils.isNotBlank(fbusmc) && fbusmc.contains(type)) {
            wjxsmc = fbusmc; // 文件显示名称
        }else {
            wjxsmc = type + "-" + fbusmc;
        }

        // 是否存在该类型的文件
        /*LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(wjxsmc),SpcySqsxWjjl::getFwjxsmc, wjxsmc);
        queryWrapper.eq(SpcySqsxWjjl::getFpid, sqsxid);
        queryWrapper.eq(SpcySqsxWjjl::getFwjlx1, type);
        List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);*/
        // 不存在，新增
        /*if (CollectionUtils.isNotEmpty(oldWjllist)) {
            wjxsmc += "("+oldWjllist.size()+")";
        }*/

        SpcySqsxWjjl newWjjl = null;
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        File newFile = new File(localFileUploadUtils.getTempFileRootUrl() + newDir);
        if (!newFile.exists()){
            newFile.mkdirs();
        }
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmbid);

        sjmc = GetUuIdUtils.NotReplaceUuId();
        wjccmc = sjmc + wjlx;
        cflj = newDir + "/" + wjccmc;
        newWjjl = new SpcySqsxWjjl();
        BeanUtil.copyProperties(wjjl, newWjjl);

        /*destUrl = localFileUploadUtils.getTempFileRootUrl() + cflj; // 保存路径
        InsertAndOutWordUtils.wordTextSubstitution(wjmbUrl, destUrl, formatType, map); // word关键字替换文本,并生成新文件*/

        String wordText = ""; // word里面的内容

        if (isWordContent) {
            /*wordText = InsertAndOutWordUtils.getWordText(destUrl, wjlx); // 获取word里面的内容
            if (org.apache.commons.lang3.StringUtils.isNotBlank(wordText)) {
                wordText = wordText.replace(BusinessConstant.HNCA_WJZ, " ");
            }*/

            wordText = genSqsxWord(sqsxid, type, cflj, wjlx, wjmbUrl, destUrl, formatType, map, true);
        }else {
            wordText = genSqsxWord(sqsxid, type, cflj, wjlx, wjmbUrl, destUrl, formatType, map, false);
        }

        if (StringUtils.isNotBlank(wordText) && wordText.equalsIgnoreCase("ERROR")){
            return Result.error("模板错误！");
        }

        newWjjl.setId(GetUuIdUtils.ReplaceUuId());
        newWjjl.setFwjxsmc(wjxsmc);
        newWjjl.setFwjccmc(wjccmc);
        newWjjl.setFcflj(cflj);
        if (isWordContent) {
            newWjjl.setFwjnr(wordText);
        }
        newWjjl.setFbusid(fbusid);
        newWjjl.setFbusmc(fbusmc);
        newWjjl.setFwjlx7(czrid);
        newWjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        /** 被合并事项id */
        if (oldWjjl != null && StringUtils.isNotBlank(oldWjjl.getFwjlx6())){
            newWjjl.setFwjlx6(oldWjjl.getFwjlx6());
        }

        spcySqsxWjjlService.save(newWjjl);

        /*// minio文件上传
        this.uploadMinioFileWjjl(cflj,destUrl);*/

        String logWj = "选择文件模板【"+type+"】初始化文件：";
        Map<String, Object> map1 = ObjectToMapUtils.objectToMap(newWjjl);
        logWj += LogUtils.MapToStringMethod(map1);
        spcyLogService.addLogs(nowNodeName, logWj, sqsxid, LogConstant.LOGS_SQSX_WJJL, SpcyConstant.LOGS_SYS_TYPE_XZ);

        return Result.ok();
    }

    /**
     * ************************************* 【 查 验 文 件 初始化】 ************************************
     */

    /** 申请事项-模板套用-生成对应附件-并上传文件桶 */
    public String genSqsxWj(String cflj, String wjlx, String wjmbUrl, String outFilePath, String formatType, Map<String, String> map){
        return genSqsxWj(cflj, wjlx, wjmbUrl, outFilePath, formatType, map, false);
    }

    /** 申请事项-模板套用-生成对应附件-并上传文件桶 */
    public String genSqsxWj(String cflj, String wjlx, String wjmbUrl, String outFilePath, String formatType, Map<String, String> map, boolean isWordText){
        outFilePath = localFileUploadUtils.getTempFileRootUrl() + cflj; // 保存路径

        InsertAndOutWordUtils.wordTextSubstitution(wjmbUrl, outFilePath, formatType, map); // word关键字替换文本,并生成新文件

        String wordText = "";
        if (isWordText) {
            wordText = InsertAndOutWordUtils.getWordText(outFilePath, wjlx); // 获取word里面的内容
            if (org.apache.commons.lang3.StringUtils.isNotBlank(wordText)) {
                wordText = wordText.replace(BusinessConstant.HNCA_WJZ, " ");
            }
        }

        // minio文件上传
        if (this.uploadMinioFileWjjl(cflj, outFilePath)){
            return wordText;
        }else {
            return "ERROR";
        }
    }

    /** 申请事项-模板套用-生成对应附件-并上传文件桶 */
    public String genSqsxWord(String sqsxid, String type, String cflj, String wjlx, String wjmbUrl, String outFilePath, String formatType, Map<String, String> map, boolean isWordText){
        outFilePath = localFileUploadUtils.getTempFileRootUrl() + cflj; // 保存路径

        if (StringUtils.isNotBlank(type) && formatType.equals("DOCX")) {
            if (type.equals(WjmlConstant.xcjcbg) || type.equals(WjmlConstant.xcjcqxjlb)) {
                ConfigureBuilder builder = Configure.builder();
                HashMap<String, Object> parMap = ObjectToMapUtils.templateDataKeyHashMapToKey(map);

                /** 首次签名 */
                HashMap<String, Object> qmMap = sqsxYwxxInfoService.getXcjcbgQmBySchy(sqsxid);
                if (qmMap != null) {
                    parMap.putAll(qmMap);
                }

                /** 检查情况转述 */
                parMap.put("jcqkzsList", spcyFlfgService.genXcjcJcqkzsContentObject(sqsxid, ""));

                /** 检查章节 */
                parMap.put("list", spcyFlfgService.genXcjczjStructureContentObject(sqsxid));

                /** 现场检查报告-中心人员签名关键字 */
                parMap.put("zxQmGjz", "${data.@zxQm}\n${data.nowTime}");

                /** 模板关键配置 ${data.xxx} */
                builder.buildGramer("${data.", "}");

                /** html插件相关字段 */
                builder.bind("fjcqkzs", new HtmlRenderPolicy());
                builder.bind("jczj", new HtmlRenderPolicy());
                builder.bind("zjms", new HtmlRenderPolicy());
                builder.bind("fxwt", new HtmlRenderPolicy());
                builder.bind("jcqkzs", new HtmlRenderPolicy());

                WordDocxReplaceUtils.docxConfigureReplace(wjmbUrl, outFilePath, builder.useSpringEL(false).build(), parMap);
            }else {
                InsertAndOutWordUtils.wordTextSubstitution(wjmbUrl, outFilePath, formatType, map); // word关键字替换文本,并生成新文件
            }
        }else {
            InsertAndOutWordUtils.wordTextSubstitution(wjmbUrl, outFilePath, formatType, map); // word关键字替换文本,并生成新文件
        }

        String wordText = "";
        if (isWordText) {
            wordText = InsertAndOutWordUtils.getWordText(outFilePath, wjlx); // 获取word里面的内容
            if (org.apache.commons.lang3.StringUtils.isNotBlank(wordText)) {
                wordText = wordText.replace(BusinessConstant.HNCA_WJZ, " ");
            }
        }

        // minio文件上传
        if (this.uploadMinioFileWjjl(cflj, outFilePath)){
            return wordText;
        }else {
            return "ERROR";
        }
    }

    /**
     * 初始化本地文件目录
     * @param ml 带前缀文件目录
     */
    public void initLocalTempFileMl(String ml){
        File newFile = new File(localFileUploadUtils.getTempFileRootUrl() + ml);
        if (!newFile.exists()){
            newFile.mkdirs();
        }
    }

    /**
     * 上传申请事项文件-minio
     * @param cflj 事项文件相对路径
     * @param absUrl 本地事项文件绝对路径
     */
    public boolean uploadMinioFileWjjl(String cflj, String absUrl){

        try {
            if (localFileUploadUtils.isEnableMinioWjjlFile()) {
                return spcyMinioUtils.putFlagFile(localFileUploadUtils.getSqsxWjjlRelativeUrl(cflj), new File(absUrl), Util.getWjHz(cflj));
            }else {
                return FileCopyUtils.copyFile(absUrl, localFileUploadUtils.getSqsxWjjlAbsUrl(cflj));
            }
        }catch (Exception e){
            log.error("上传路径：{}; 被上传文件路径：{}", cflj, absUrl);
            log.error("上传申请事项文件-minio错误：{}", e.getMessage());

            return false;
        }
    }

    /**
     * 获取文件模板路径
     * @param wjmbId 文件模板id
     * @param sqsxid 申请事项id
     * @param sqsx 申请事项
     * @param fywflcode 业务分类编码
     * @param type 模板类型
     *
     * @return 返回文件模板绝对路径
     */
    public SqsxWjmbVo getHbsxWjmbBySqsxId(String wjmbId, String sqsxid, SpcySqsxBasic sqsx, String fywflcode, String type, String bhbsqsxids){
        return this.getCommonWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type, bhbsqsxids);
    }

    public SqsxWjmbVo getWjmbBySqsxId(String wjmbId, String sqsxid, SpcySqsxBasic sqsx, String fywflcode, String type){
        return this.getCommonWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type, "");
    }

    /**
     * 获取文件模板路径
     * @param wjmbId 文件模板id
     * @param sqsxid 申请事项id
     * @param sqsx 申请事项
     * @param fywflcode 业务分类编码
     * @param type 模板类型
     *
     * @return 返回文件模板绝对路径
     */
    private SqsxWjmbVo getCommonWjmbBySqsxId(String wjmbId, String sqsxid, SpcySqsxBasic sqsx, String fywflcode, String type, String bhbsqsxids){
        if (sqsx == null) {
            sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        }

        SpcyRsWjmb wjmb = null;
        String wjmbid = "";
        String wjmbUrl = "";

        if (StringUtils.isBlank(type) && StringUtils.isNotBlank(wjmbId)){
            type = spcyRsWjmbService.selectFmblx1ById(wjmbId);
        }

        if (StringUtils.isNotBlank(type)) {
            if (type.equals(WjmlConstant.xcjcbg)) {

                /** 申请事项选择的报告模板 */
                List<SpcyRsWjmb> list = new ArrayList<>();
                /** 验证是否为合并事项 */
                if (StringUtils.isNotBlank(sqsx.getFhbsxids())){
                    // 合并事项
                    list = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                            .eq(SpcyRsWjmb::getFmblx1, type)
                            .like(SpcyRsWjmb::getFmblx3, sqsxid));
                    if (CollectionUtil.isEmpty(list)){
                        list = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                                .eq(SpcyRsWjmb::getFsqsxid, sqsxid)
                                .eq(SpcyRsWjmb::getFmblx1, type));
                    }

                }else {
                    list = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                            .eq(SpcyRsWjmb::getFsqsxid, sqsxid)
                            .eq(SpcyRsWjmb::getFmblx1, type));
                }

                if (CollectionUtil.isNotEmpty(list)) {
                    String sqsxMbUrl = "";
                    // 是否开始minio
                    if (localFileUploadUtils.isEnableMinioWjmbFile()) {
                        sqsxMbUrl = localFileUploadUtils.convertMinioToFile(localFileUploadUtils.getWjmbRelativeUrl(list.get(0).getFcflj()), list.get(0).getFwjlx()).getAbsolutePath();
                    } else {
                        sqsxMbUrl = localFileUploadUtils.getWjmbAbsUrl(list.get(0).getFcflj()); // 模板路径

                        /** doc转docx */
                        if (list.get(0).getFwjlx().equalsIgnoreCase(".doc")){
                            String outUrl = localFileUploadUtils.getTempFileRootUrl() + "/" + GetUuIdUtils.NotReplaceUuId() + ".docx";
                            try {
                                WordPdfUtils.wordToDocx(sqsxMbUrl, outUrl);
                                sqsxMbUrl = outUrl;
                            }catch (Exception e){
                                log.error("doc文档转docx文档错误：{}", e.getMessage());
                            }

                        }
                    }

                    /** 存在文件 */
                    if (new File(sqsxMbUrl).exists()) {
                        wjmb = list.get(0);
                        wjmb.setFwjlx(Util.getWjHz(sqsxMbUrl));
                        wjmbUrl = sqsxMbUrl;
                    }
                }
            }
        }

        if (wjmb == null) {
            if (StringUtils.isNotBlank(wjmbId)) {
                wjmb = spcyRsWjmbService.getById(wjmbId);
                if (wjmb == null || StringUtils.isBlank(wjmb.getFcflj())) {
                    log.error("当前选择的文件模板不存在！请重新选择！");

                    return new SqsxWjmbVo("当前选择的文件模板不存在！请重新选择！");
                }
            } else {
                // 获取文件模板
                Result result = this.getWjmbByYwflcode(sqsx, fywflcode, type);
                if (!result.isSuccess()) {
                    log.error(result.getMessage());

                    return new SqsxWjmbVo(result.getMessage());
                } else {
                    wjmb = (SpcyRsWjmb) result.getResult();
                }

            }

            // 是否开始minio
            if (localFileUploadUtils.isEnableMinioWjmbFile()){
                File file = localFileUploadUtils.convertMinioToFile(localFileUploadUtils.getWjmbRelativeUrl(wjmb.getFcflj()), wjmb.getFwjlx());
                if (file != null) {
                    wjmbUrl = file.getAbsolutePath();
                }
            }else {
                wjmbUrl = localFileUploadUtils.getWjmbAbsUrl(wjmb.getFcflj()); // 模板路径

                /** doc转docx */
                if (wjmb.getFwjlx().equalsIgnoreCase(".doc")){
                    String outUrl = localFileUploadUtils.getTempFileRootUrl() + "/" + GetUuIdUtils.NotReplaceUuId() + ".docx";
                    try {
                        WordPdfUtils.wordToDocx(wjmbUrl, outUrl);
                        wjmbUrl = outUrl;
                        wjmb.setFwjlx(".docx");
                    }catch (Exception e){
                        log.error("doc文档转docx文档错误：{}", e.getMessage());
                    }

                }
            }
        }

        wjmbid = wjmb.getId();
        return new SqsxWjmbVo(wjmbid, wjmb, wjmbUrl);
    }

    /**
     * 初始化-选派现场审查员的函
     * @param sqsxid 申请事项id
     * @param type 模板类型
     * @param wjmbId 文件模板id
     */
    public Result initXpxcscydh(String sqsxid,String type,String wjmbId){
        // 获取同意选派人员
        List<SpcySqsxXpjl> xpList = spcySqsxXpjlService.getTyXpXpjlList(sqsxid, null);
        if (CollectionUtils.isEmpty(xpList)){
            return Result.error("请选择【同意】选派的检查人员, 再进行初始化文件。");
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        SpcyRsWjmb wjmb = null; // 第一个文件模板
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        for (SpcySqsxXpjl xpjl : xpList) {
            // 是否存在该类型的文件
            LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SpcySqsxWjjl::getFpid, sqsxid);
            queryWrapper.eq(SpcySqsxWjjl::getFwjlx1, type);
            queryWrapper.eq(SpcySqsxWjjl::getFbusid, xpjl.getId());
            List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);
            if (CollectionUtils.isNotEmpty(oldWjllist)) {
                continue;
            }

            map = getObjectMap(sqsxid, xpjl.getId());
            wjxsmc = type + "-" + xpjl.getFjcyxm(); // 文件显示名称

            sjmc = GetUuIdUtils.NotReplaceUuId();
            wjccmc = sjmc + wjlx;
            cflj = newDir + "/" + wjccmc;
            newWjjl = new SpcySqsxWjjl();
            BeanUtil.copyProperties(wjjl, newWjjl);

            String wordText = genSqsxWj(cflj, wjlx, wjmbUrl, destUrl, formatType, map, true);

            this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, xpjl.getId(), xpjl.getFjcyxm(), xpjl.getFuserid());

            /*String logWj = "初始化选派现场审查员的函文件：";
            Map<String, Object> map1 = ObjectToMapUtils.objectToMap(newWjjl);
            logWj += LogUtils.MapToStringMethod(map1);
            spcyLogService.addLogs(nowNodeName, logWj, sqsxid, LogConstant.LOGS_SQSX_WJJL, SpcyConstant.LOGS_SYS_TYPE_XZ);*/
        }


        return Result.ok();
    }

    /**
     * 删除相关初始化生产函文件
     * @param fbusid 文件记录业务id
     * @param fpid 业务id
     * @param type 文件类型
     * */
    public void deleteXgInitScFile(String fbusid, String fpid, String type){
        List<SpcySqsxWjjl> list = new ArrayList<>();

        if (StringUtils.isNotBlank(fbusid)) {
            list = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                    .eq(SpcySqsxWjjl::getFbusid, fbusid));
        }else {
            list = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                    .eq(SpcySqsxWjjl::getFpid, fpid)
                    .eq(SpcySqsxWjjl::getFwjlx1, type));
        }

        if (CollectionUtil.isEmpty(list)){
            return;
        }

        for (SpcySqsxWjjl oldWj : list) {
            if (StringUtils.isNotBlank(oldWj.getFwjlx4())
                    && oldWj.getFwjlx4().equals(SpcyCommonConstant.SQSX_WJJL_TYPE4_SDSC)) {
                // 不处理
            }else {
                if (StringUtils.isNotBlank(oldWj.getFcflj())) {
                    localFileUploadUtils.deleteLocalFileUrl(oldWj.getFcflj());
                }
                if (StringUtils.isNotBlank(oldWj.getFjdcflj())) {
                    localFileUploadUtils.deleteLocalFileUrl(oldWj.getFjdcflj());
                }
                spcySqsxWjjlService.removeById(oldWj.getId());
            }
        }
    }

    /**
     * 初始化-选派现场观察员的函
     * @param sqsxid 申请事项id
     * @param type 模板类型
     */
    public Result initXpxcgcydh(String sqsxid, String type, String wjmbId){
        // 是否存在该类型的文件
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid, sqsxid);
        queryWrapper.eq(SpcySqsxWjjl::getFwjlx1, type);
        List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);
        // 不存在，新增
        if (CollectionUtils.isNotEmpty(oldWjllist)) {
            return Result.error("生成失败！已经存在该材料文件了！");
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }

        if (StringUtils.isBlank(sqsx.getFcygcyid())){
            return Result.error("没有选择观察员！");
        }

        String fywflcode = sqsx.getFywflcode();

        SpcyRsWjmb wjmb = null;
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        String fcygcyid = sqsx.getFcygcyid();
        List<SpcyRsGcy> gcyList = spcyRsGcyService.list(new LambdaQueryWrapper<SpcyRsGcy>()
                .eq(StringUtils.isNotBlank(fcygcyid), SpcyRsGcy::getId, fcygcyid));
        if (CollectionUtil.isEmpty(gcyList)){
            return Result.ok("初始化成功");
        }

        SpcyRsGcy gcy = gcyList.get(0);
        wjxsmc = type + "-" + gcy.getFdwmc(); // 文件显示名称

        sjmc = GetUuIdUtils.NotReplaceUuId();
        wjccmc = sjmc + wjlx;
        cflj = newDir + "/" + wjccmc;
        newWjjl = new SpcySqsxWjjl();
        BeanUtil.copyProperties(wjjl, newWjjl);

        map = getObjectMap(sqsxid,"");
        String wordText = genSqsxWj(cflj, wjlx, wjmbUrl, destUrl, formatType, map, true);

        this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, gcy.getId(), gcy.getFgcyxm());

        return Result.ok("初始化成功");
    }

    /**
     * 初始化-协助检查函
     * @param sqsxid 申请事项id
     * @param type 模板类型
     */
    public Result initXzjch(String sqsxid,String type,String wjmbId){
        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        SpcyRsWjmb wjmb = null;
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        map = getObjectMap(sqsxid,"");

        String fcygcyid = sqsx.getFcygcyid();
        List<SpcyRsGcy> gcyList = spcyRsGcyService.list(new LambdaQueryWrapper<SpcyRsGcy>()
                .eq(StringUtils.isNotBlank(fcygcyid), SpcyRsGcy::getId, fcygcyid));
        if (CollectionUtils.isNotEmpty(gcyList)){
            SpcyRsGcy gcy = gcyList.get(0);
            wjxsmc = type+"-"+gcy.getFdwmc(); // 文件显示名称

            // 是否存在该类型的文件
            LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SpcySqsxWjjl::getFpid,sqsxid);
            queryWrapper.eq(SpcySqsxWjjl::getFwjlx1,type);
            List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);
            // 不存在，新增
            if (CollectionUtils.isNotEmpty(oldWjllist)) {
                throw new BusinessException("生成失败！已经存在该材料文件了！");
            }

            sjmc = GetUuIdUtils.NotReplaceUuId();
            wjccmc = sjmc + wjlx;
            cflj = newDir + "/" + wjccmc;
            newWjjl = new SpcySqsxWjjl();
            BeanUtil.copyProperties(wjjl, newWjjl);

            String wordText = genSqsxWj(cflj, wjlx, wjmbUrl, destUrl, formatType, map, true);

            this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, gcy.getId(), gcy.getFgcyxm());

        }
        return Result.ok("初始化成功");
    }

    /**
     * 初始化-观察员声明
     * @param sqsxid 申请事项id
     * @param type 模板类型
     */
    public Result initGcysm(String sqsxid,String type,String wjmbId){
        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        SpcyRsWjmb wjmb = null;
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        map = getObjectMap(sqsxid,"");

        String fcygcyid = sqsx.getFcygcyid();
        List<SpcyRsGcy> gcyList = spcyRsGcyService.list(new LambdaQueryWrapper<SpcyRsGcy>()
                .eq(StringUtils.isNotBlank(fcygcyid), SpcyRsGcy::getId, fcygcyid));
        if (CollectionUtils.isNotEmpty(gcyList)){
            SpcyRsGcy gcy = gcyList.get(0);
            wjxsmc = type+"-"+gcy.getFdwmc(); // 文件显示名称

            // 是否存在该类型的文件
            LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SpcySqsxWjjl::getFpid,sqsxid);
            queryWrapper.eq(SpcySqsxWjjl::getFwjlx1,type);
            List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);

            // 不存在，新增
            if (CollectionUtils.isNotEmpty(oldWjllist)) {
                throw new BusinessException("生成失败！已经存在该材料文件了！");
            }

            sjmc = GetUuIdUtils.NotReplaceUuId();
            wjccmc = sjmc + wjlx;
            cflj = newDir + "/" + wjccmc;
            newWjjl = new SpcySqsxWjjl();
            BeanUtil.copyProperties(wjjl, newWjjl);

            String wordText = genSqsxWj(cflj, wjlx, wjmbUrl, destUrl, formatType, map, true);

            this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, gcy.getId(), gcy.getFgcyxm());

        }
        return Result.ok("初始化成功");
    }

    /**
     * 初始化 - 企业单位检查通知
     * @param sqsxid 申请事项id
     * @param type 模板类型
     */
    public Result initQydwjctz(String sqsxid,String type,String wjmbId){
        // 是否存在该类型的文件
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid,sqsxid);
        queryWrapper.eq(SpcySqsxWjjl::getFwjlx1,type);
        List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);
        // 不存在，新增
        if (CollectionUtils.isNotEmpty(oldWjllist)) {
//            throw new BusinessException("生成失败！已经存在该材料文件了！");
            return Result.error(type+"生成失败！已经存在该材料文件了！");
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        SpcyRsWjmb wjmb = null;
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        map = getObjectMap(sqsxid,""); // 获取替换相关内容

        String fqymc = sqsx.getFqymc();
        wjxsmc = type+"-"+fqymc; // 文件显示名称

        sjmc = GetUuIdUtils.NotReplaceUuId();
        wjccmc = sjmc + wjlx;
        cflj = newDir + "/" + wjccmc;

        newWjjl = new SpcySqsxWjjl();
        BeanUtil.copyProperties(wjjl, newWjjl);

        destUrl = localFileUploadUtils.getTempFileRootUrl() + cflj; // 保存路径

        String wordText = genSqsxWj(cflj, wjlx, wjmbUrl, destUrl, formatType, map, true);

        this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, sqsx.getFqyid(), fqymc);

        return Result.ok();
    }

    /**
     * 初始化 - 检查方案
     * @param sqsxid 申请事项id
     * @param type 模板类型
     */
    public Result initJcfa(String sqsxid,String type,String wjmbId){
        // 是否存在该类型的文件
        boolean typeFlag = true;
        if (StringUtils.isNotBlank(type)) {
            typeFlag = false;
            LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SpcySqsxWjjl::getFpid, sqsxid);
            queryWrapper.eq(SpcySqsxWjjl::getFwjlx1, type);
            List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);
            // 不存在，新增
            if (CollectionUtils.isNotEmpty(oldWjllist)) {
                return Result.error(type + "已经存在该材料文件了！");
            }
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        SpcyRsWjmb wjmb = null; //  第一个文件模板
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();
            if (StringUtils.isBlank(type)) {
                type = wjmb.getFmblx1();
            }

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        if (typeFlag) {
            LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SpcySqsxWjjl::getFpid, sqsxid);
            queryWrapper.eq(SpcySqsxWjjl::getFwjlx1, type);
            List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);
            // 不存在，新增
            if (CollectionUtils.isNotEmpty(oldWjllist)) {
                return Result.error(type + "已经存在该材料文件了！");
            }
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型

        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        String fqymc = sqsx.getFqymc();
        wjxsmc = type+"-"+fqymc; // 文件显示名称

        sjmc = GetUuIdUtils.NotReplaceUuId();
        wjccmc = sjmc + wjlx;
        cflj = newDir + "/" + wjccmc;
        newWjjl = new SpcySqsxWjjl();
        BeanUtil.copyProperties(wjjl, newWjjl);

        Map map = getObjectMap(sqsxid,""); // 获取替换相关内容
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式
        String wordText = genSqsxWj(cflj, wjlx, wjmbUrl, destUrl, formatType, map, false);

        this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, sqsxid, wjxsmc);

        return Result.ok();
    }

    /**
     * 初始化 - 无利益冲突声明
     */
    public Result initWlyctsm(String sqsxid,String type,String wjmbId){
        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        SpcyRsWjmb wjmb = null; // 第一个文件模板
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        List<SpcySqsxXpjl> xpList = spcySqsxXpjlService.list(new LambdaQueryWrapper<SpcySqsxXpjl>()
                .eq(SpcySqsxXpjl::getFsqsxid, sqsxid)
                .eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_YES)
                .eq(SpcySqsxXpjl::getFspcylx, SpcyConstant.FLOW_TYPE_CY)
                .orderByAsc(SpcySqsxXpjl::getFzpsj));
        if (CollectionUtils.isNotEmpty(xpList)) {
            for (SpcySqsxXpjl xpjl : xpList) {
                map = getObjectMap(sqsxid,xpjl.getId());
                wjxsmc = type+"-"+xpjl.getFjcyxm(); // 文件显示名称

                // 是否存在该类型的文件
                LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(SpcySqsxWjjl::getFwjxsmc, wjxsmc);
                queryWrapper.eq(SpcySqsxWjjl::getFpid,sqsxid);
                queryWrapper.eq(SpcySqsxWjjl::getFwjlx1,type);
                List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);

                if (CollectionUtils.isNotEmpty(oldWjllist)) {
                    continue;
                }

                sjmc = GetUuIdUtils.NotReplaceUuId();
                wjccmc = sjmc + wjlx;
                cflj = newDir + "/" + wjccmc;
                newWjjl = new SpcySqsxWjjl();
                BeanUtil.copyProperties(wjjl, newWjjl);

                String wordText = genSqsxWj(cflj, wjlx, wjmbUrl, destUrl, formatType, map, false);

                this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, xpjl.getId(), xpjl.getFjcyxm(), xpjl.getFuserid());

            }
        }
        return Result.ok("初始化成功");
    }

    /**
     * 初始化 - 现场检查检查员承诺书
     */
    public Result initXcjcjcycns(String sqsxid,String type,String wjmbId){
        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        SpcyRsWjmb wjmb = null;
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        List<SpcySqsxXpjl> xpList = spcySqsxXpjlService.list(new LambdaQueryWrapper<SpcySqsxXpjl>()
                .eq(SpcySqsxXpjl::getFsqsxid, sqsxid)
                .eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_YES)
                .eq(SpcySqsxXpjl::getFspcylx, SpcyConstant.FLOW_TYPE_CY)
                .orderByAsc(SpcySqsxXpjl::getFzpsj));
        if (CollectionUtils.isNotEmpty(xpList)) {
            for (SpcySqsxXpjl xpjl : xpList) {
                map = getObjectMap(sqsxid,xpjl.getId());
                wjxsmc = type+"-"+xpjl.getFjcyxm(); // 文件显示名称

                // 是否存在该类型的文件
                LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(SpcySqsxWjjl::getFwjxsmc, wjxsmc);
                queryWrapper.eq(SpcySqsxWjjl::getFpid,sqsxid);
                queryWrapper.eq(SpcySqsxWjjl::getFwjlx1,type);
                List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);

                if (CollectionUtils.isNotEmpty(oldWjllist)) {
                    continue;
                }

                sjmc = GetUuIdUtils.NotReplaceUuId();
                wjccmc = sjmc + wjlx;
                cflj = newDir + "/" + wjccmc;
                newWjjl = new SpcySqsxWjjl();
                BeanUtil.copyProperties(wjjl, newWjjl);

                String wordText = genSqsxWj(cflj, wjlx, wjmbUrl, destUrl, formatType, map, false);

                this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, xpjl.getId(), xpjl.getFjcyxm(), xpjl.getFuserid());

            }
        }
        return Result.ok("初始化成功");
    }

    /**
     * 初始化 - 现场检查纪律
     */
    public Result initXcjcjl(String sqsxid,String type,String wjmbId){
        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        SpcyRsWjmb wjmb = null;
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        map = getObjectMap(sqsxid,""); // 获取替换相关内容

        wjxsmc = type; // 文件显示名称

        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser != null){
            wjxsmc += "-" + currentUser.getName();
        }

        // 是否存在该类型的文件
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid,sqsxid);
        queryWrapper.eq(SpcySqsxWjjl::getFwjlx1,type);
        List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);

        // 不存在，新增
        if (CollectionUtils.isNotEmpty(oldWjllist)) {
            throw new BusinessException("生成失败！已经存在该材料文件了！");
        }

        sjmc = GetUuIdUtils.NotReplaceUuId();
        wjccmc = sjmc + wjlx;
        cflj = newDir + "/" + wjccmc;
        newWjjl = new SpcySqsxWjjl();
        BeanUtil.copyProperties(wjjl, newWjjl);

        String wordText = genSqsxWj(cflj, wjlx, wjmbUrl, destUrl, formatType, map, false);

        this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, sqsxid, wjxsmc);

        return Result.ok("初始化成功");
    }

    /**
     * 初始化 - 现场检查会议签到表
     */
    public Result initXcjchyqdb(String sqsxid,String type,String wjmbId){

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        SpcyRsWjmb wjmb = null;
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        map = getObjectMap(sqsxid,""); // 获取替换相关内容

        wjxsmc = type; // 文件显示名称

        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser != null){
            wjxsmc += "-"+currentUser.getName();
        }

        // 是否存在该类型的文件
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid,sqsxid);
        queryWrapper.eq(SpcySqsxWjjl::getFwjlx1,type);
        List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);

        // 不存在，新增
        if (CollectionUtils.isNotEmpty(oldWjllist)) {
            throw new BusinessException("生成失败！已经存在该材料文件了！");
        }

        sjmc = GetUuIdUtils.NotReplaceUuId();
        wjccmc = sjmc + wjlx;
        cflj = newDir + "/" + wjccmc;
        newWjjl = new SpcySqsxWjjl();
        BeanUtil.copyProperties(wjjl, newWjjl);

        String wordText = genSqsxWj(cflj, wjlx, wjmbUrl, destUrl, formatType, map, false);

        this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, sqsxid, wjxsmc);

        return Result.ok("初始化成功");
    }

    /**
     * 初始化 - 现场检查报告
     */
    public Result initXcjcbg(String sqsxid,String type,String wjmbId){
        // 是否存在该类型的文件
        boolean typeFlag = true;
        if (StringUtils.isNotBlank(type)) {
            typeFlag = false;
            LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SpcySqsxWjjl::getFpid, sqsxid);
            queryWrapper.eq(SpcySqsxWjjl::getFwjlx1, type);
            List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);
            // 不存在，新增
            if (CollectionUtils.isNotEmpty(oldWjllist)) {
                return Result.error(type + "已经存在该材料文件了！");
            }
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        /**
         * 构建检查项目-缺陷信息
         *  同步检查项目信息
         */
        SqsxXcjcQxParamBo jcxmQxxx = spcyFlfgService.getJcxmQxxx(sqsxid, false);
        spcySqsxBaiscService.updateSqsxQxxx(sqsxid, jcxmQxxx);

        SpcyRsWjmb wjmb = null; //  第一个文件模板
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();
            if (StringUtils.isBlank(type)) {
                type = wjmb.getFmblx1();
            }

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        // 是否存在该类型的文件
        if (typeFlag) {
            LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SpcySqsxWjjl::getFpid, sqsxid);
            queryWrapper.eq(SpcySqsxWjjl::getFwjlx1, type);
            List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);
            // 不存在，新增
            if (CollectionUtils.isNotEmpty(oldWjllist)) {
                return Result.error(type + "已经存在该材料文件了！");
            }
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        map = getObjectMap(sqsxid,""); // 获取替换相关内容
        /*if (StringUtils.isNotBlank(fywflcode)){
            // 药品流通 模板字段
            if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPJY)){
                map.putAll(this.mbFieldByYplt(sqsxid));
                map.putAll(this.mbFieldQkhzByYplt(sqsxid)); // 情况汇总评估
            }
        }*/

        /** 获取现场章节描述-通过申请事项id */
        /*try {
            map.putAll(spcyFlfgService.genXcjczjStructureContent(sqsxid));
        }catch (Exception e){
            log.error("获取文件模板章节描述-通过申请事项id: {}", e.getMessage());
        }*/

        String fqymc = sqsx.getFqymc();
        wjxsmc = type+"-"+fqymc; // 文件显示名称

        sjmc = GetUuIdUtils.NotReplaceUuId();
        wjccmc = sjmc + wjlx;
        cflj = newDir + "/" + wjccmc;
        newWjjl = new SpcySqsxWjjl();
        BeanUtil.copyProperties(wjjl, newWjjl);

//        String wordText = genSqsxWj(cflj, wjlx, wjmbUrl, destUrl, formatType, map, false);
        String wordText = genSqsxWord(sqsxid, type, cflj, wjlx, wjmbUrl, destUrl, formatType, map, false);

        this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, sqsxid, fqymc);

        return Result.ok("初始化成功");
    }

    /**
     * 初始化 - 现场检查笔录
     */
    public Result initXcjcbl(String sqsxid,String type,String wjmbId){
        return this.initXcjcbl(sqsxid, type, wjmbId, true);
    }
    public Result initXcjcbl(String sqsxid,String type,String wjmbId, boolean isVerifyExistFile){
        // 是否存在该类型的文件
        if (isVerifyExistFile) {
            LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SpcySqsxWjjl::getFpid, sqsxid);
            queryWrapper.eq(SpcySqsxWjjl::getFwjlx1, type);
            List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);
            // 不存在，新增
            if (CollectionUtils.isNotEmpty(oldWjllist)) {
                return Result.error("已经存在该材料文件了！");
            }
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        SpcyRsWjmb wjmb = null; //  第一个文件模板
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        map = getObjectMap(sqsxid,""); // 获取替换相关内容
        if (StringUtils.isNotBlank(sqsx.getFywflcode())){
            if (sqsx.getFywflcode().equals(SpcyConstant.YWFL_CODE_HZP)){
                map.putAll(this.mdFieldXcjcblByHzp(sqsxid));
            }
        }

        String fqymc = sqsx.getFqymc();
        wjxsmc = type+"-"+fqymc; // 文件显示名称

        sjmc = GetUuIdUtils.NotReplaceUuId();
        wjccmc = sjmc + wjlx;
        cflj = newDir + "/" + wjccmc;
        newWjjl = new SpcySqsxWjjl();
        BeanUtil.copyProperties(wjjl, newWjjl);

        String wordText = genSqsxWj(cflj, wjlx, wjmbUrl, destUrl, formatType, map, false);

        this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, sqsxid, fqymc);

        return Result.ok("初始化成功");
    }

    /**
     * 初始化 - 现场检查结论
     */
    public Result initHzpXcjcjl(String sqsxid,String type,String wjmbId){
        // 是否存在该类型的文件
        boolean typeFlag = true;
        if (StringUtils.isNotBlank(type)) {
            typeFlag = false;
            LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SpcySqsxWjjl::getFpid, sqsxid);
            queryWrapper.eq(SpcySqsxWjjl::getFwjlx1, type);
            List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);
            // 不存在，新增
            if (CollectionUtils.isNotEmpty(oldWjllist)) {
                return Result.error(type + "已经存在该材料文件了！");
            }
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        /**
         * 构建检查项目-缺陷信息
         *  同步检查项目信息
         */
        SqsxXcjcQxParamBo jcxmQxxx = spcyFlfgService.getJcxmQxxx(sqsxid, false);
        spcySqsxBaiscService.updateSqsxQxxx(sqsxid, jcxmQxxx);

        SpcyRsWjmb wjmb = null; //  第一个文件模板
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();
            if (StringUtils.isBlank(type)){
                type = wjmb.getFmblx1();
            }

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        if (typeFlag) {
            LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SpcySqsxWjjl::getFpid, sqsxid);
            queryWrapper.eq(SpcySqsxWjjl::getFwjlx1, type);
            List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);
            // 不存在，新增
            if (CollectionUtils.isNotEmpty(oldWjllist)) {
                return Result.error(type + "已经存在该材料文件了！");
            }
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        map = getObjectMap(sqsxid,""); // 获取替换相关内容
        if (StringUtils.isNotBlank(fywflcode)){
            if (fywflcode.equals(SpcyConstant.YWFL_CODE_HZP)){
                map.putAll(this.mdFieldXcjcblByHzp(sqsxid));
            }
        }

        String fqymc = sqsx.getFqymc();
        wjxsmc = type+"-"+fqymc; // 文件显示名称



        sjmc = GetUuIdUtils.NotReplaceUuId();
        wjccmc = sjmc + wjlx;
        cflj = newDir + "/" + wjccmc;
        newWjjl = new SpcySqsxWjjl();
        BeanUtil.copyProperties(wjjl, newWjjl);

        String wordText = genSqsxWj(cflj, wjlx, wjmbUrl, destUrl, formatType, map, false);

        this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, sqsxid, fqymc);

        return Result.ok("初始化成功");
    }

    /**
     * 初始化 - 综合评定报告书
     */
    public Result initZhpdbgs(String sqsxid,String type,String wjmbId){
        // 是否存在该类型的文件
        boolean typeFlag = true;
        if (StringUtils.isNotBlank(type)) {
            typeFlag = false;
            LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SpcySqsxWjjl::getFpid, sqsxid);
            queryWrapper.eq(SpcySqsxWjjl::getFwjlx1, type);
            List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);
            // 不存在，新增
            if (CollectionUtils.isNotEmpty(oldWjllist)) {
                return Result.error(type + "已经存在该材料文件了！");
            }
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        /**
         * 构建检查项目-缺陷信息
         *  同步检查项目信息
         */
        SqsxXcjcQxParamBo jcxmQxxx = spcyFlfgService.getJcxmQxxx(sqsxid, false);
        spcySqsxBaiscService.updateSqsxQxxx(sqsxid, jcxmQxxx);

        SpcyRsWjmb wjmb = null;
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();
            if (StringUtils.isNotBlank(type)){
                type = wjmb.getFmblx1();
            }

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        if (typeFlag) {
            LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SpcySqsxWjjl::getFpid, sqsxid);
            queryWrapper.eq(SpcySqsxWjjl::getFwjlx1, type);
            List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);
            // 不存在，新增
            if (CollectionUtils.isNotEmpty(oldWjllist)) {
                return Result.error(type + "已经存在该材料文件了！");
            }
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        map = getObjectMap(sqsxid,""); // 获取替换相关内容
        /*if (StringUtils.isNotBlank(fywflcode)){
            // 药品流通 模板字段
            if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPJY)){
                map.putAll(this.mbFieldByYplt(sqsxid));
            }
        }*/

        String fqymc = sqsx.getFqymc();
        wjxsmc = type+"-"+fqymc; // 文件显示名称

        sjmc = GetUuIdUtils.NotReplaceUuId();
        wjccmc = sjmc + wjlx;
        cflj = newDir + "/" + wjccmc;
        newWjjl = new SpcySqsxWjjl();
        BeanUtil.copyProperties(wjjl, newWjjl);

        String wordText = genSqsxWj(cflj, wjlx, wjmbUrl, destUrl, formatType, map, false);

        this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, sqsxid, fqymc);

        return Result.ok("初始化成功");
    }

    /**
     * 初始化 - 廉洁自律反馈表
     */
    public Result initLjzlfkb(String sqsxid,String type,String wjmbId){

        // 是否存在该类型的文件
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid,sqsxid);
        queryWrapper.eq(SpcySqsxWjjl::getFwjlx1,type);
        List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);

        // 不存在，新增
        if (CollectionUtils.isNotEmpty(oldWjllist)) {
            return Result.error("已经存在该材料文件了！");
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        SpcyRsWjmb wjmb = null;
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        String fqymc = sqsx.getFqymc();
        wjxsmc = type + "-" + fqymc; // 文件显示名称

        sjmc = GetUuIdUtils.NotReplaceUuId();
        wjccmc = sjmc + wjlx;
        cflj = newDir + "/" + wjccmc;
        newWjjl = new SpcySqsxWjjl();
        BeanUtil.copyProperties(wjjl, newWjjl);
        destUrl = localFileUploadUtils.getTempFileRootUrl() + cflj; // 保存路径

        FileCopyUtils.copyFile(wjmbUrl, destUrl);

        this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, "", sqsxid, wjxsmc);

        // minio文件上传
        this.uploadMinioFileWjjl(cflj, destUrl);

        return Result.ok("初始化成功");
    }

    /**
     * 初始化 - 通用文件模板
     */
    public Result initCommonSqsxWjmb(String sqsxid, String type, String wjmbId){
        return initSyncCommonSqsxWjmb(sqsxid, type, wjmbId, true);
    }
    /**
     * 初始化 - 通用文件模板
     * @param verifyExistsFile 验证是否存在文件
     */
    public Result initSyncCommonSqsxWjmb(String sqsxid, String type, String wjmbId, boolean verifyExistsFile){

        // 是否存在该类型的文件
        boolean typeFlag = true;
        if (StringUtils.isNotBlank(type) && verifyExistsFile) {
            typeFlag = false;
            LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SpcySqsxWjjl::getFpid, sqsxid);
            queryWrapper.eq(SpcySqsxWjjl::getFwjlx1, type);
            List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);
            // 不存在，新增
            if (CollectionUtils.isNotEmpty(oldWjllist)) {
                return Result.error(type + "已经存在该材料文件了！");
            }
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        /**
         * 构建检查项目-缺陷信息
         *  同步检查项目信息
         */
        try {
            SqsxXcjcQxParamBo jcxmQxxx = spcyFlfgService.getJcxmQxxx(sqsxid, false);
            spcySqsxBaiscService.updateSqsxQxxx(sqsxid, jcxmQxxx);
        }catch (Exception e){
            log.error("构建检查项目-缺陷信息-错误：{}", e.getMessage());
        }


        SpcyRsWjmb wjmb = null;
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();

            if (StringUtils.isBlank(type)){
                type = wjmb.getFmblx1();
            }
        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        if (typeFlag && verifyExistsFile) {
            LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SpcySqsxWjjl::getFpid, sqsxid);
            queryWrapper.eq(SpcySqsxWjjl::getFwjlx1, type);
            List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);
            // 不存在，新增
            if (CollectionUtils.isNotEmpty(oldWjllist)) {
                return Result.error(type + "已经存在该材料文件了！");
            }
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        map = getObjectMap(sqsxid,""); // 获取替换相关内容
        /*if (StringUtils.isNotBlank(fywflcode)){
            // 药品流通 模板字段
            if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPJY)){
                map.putAll(this.mbFieldByYplt(sqsxid));

            }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_HZP)){
                map.putAll(this.mdFieldXcjcblByHzp(sqsxid));
            }
        }*/

        String fqymc = sqsx.getFqymc();
        wjxsmc = type + "-" + fqymc; // 文件显示名称

       /* String czrid = ""; // 操作人id
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser != null){
            czrid = currentUser.getId();
            wjxsmc += "-"+currentUser.getName();
        }*/

        // 重置-文件显示名称
        /*if (StringUtils.isNotBlank(fywflcode) && fywflcode.equals(SpcyConstant.YWFL_CODE_YLQX)){
            wjxsmc = fsxmc + "-" + sqsx.getFsplx();
        }*/

        sjmc = GetUuIdUtils.NotReplaceUuId();
        wjccmc = sjmc + wjlx;
        cflj = newDir + "/" + wjccmc;
        newWjjl = new SpcySqsxWjjl();
        BeanUtil.copyProperties(wjjl, newWjjl);

        String wordText = genSqsxWord(sqsxid, type, cflj, wjlx, wjmbUrl, destUrl, formatType, map, false);

        this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, sqsxid, wjxsmc);

        return Result.ok("初始化成功");
    }

    /**
     * 初始化 - 其他业务绑定通用文件模板
     * */
    public Result initSyncOtherCommonWjmb(String fpid, String sqsxid, String type, String wjmbId, boolean isDelWj){

        List<SpcySqsxWjjl> oldWjList = null;
        if (isDelWj){
            oldWjList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                    .eq(SpcySqsxWjjl::getFpid, fpid)
                    .eq(SpcySqsxWjjl::getFwjlx1, type));
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        SpcyRsWjmb wjmb = null;
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();

            if (StringUtils.isBlank(type)){
                type = wjmb.getFmblx1();
            }
        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(fpid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        map = getObjectMap(sqsxid,""); // 获取替换相关内容

        String fqymc = sqsx.getFqymc();
        wjxsmc = type + "-" + fqymc; // 文件显示名称

        sjmc = GetUuIdUtils.NotReplaceUuId();
        wjccmc = sjmc + wjlx;
        cflj = newDir + "/" + wjccmc;
        newWjjl = new SpcySqsxWjjl();
        BeanUtil.copyProperties(wjjl, newWjjl);

        String wordText = genSqsxWord(sqsxid, type, cflj, wjlx, wjmbUrl, destUrl, formatType, map, false);

        this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, sqsxid, wjxsmc);

        if (isDelWj){
            if (CollectionUtil.isNotEmpty(oldWjList)) {
                for (SpcySqsxWjjl oldWj : oldWjList) {
                    spcySqsxWjjlService.removeById(oldWj.getId());

                    if (StringUtils.isNotBlank(oldWj.getFcflj())) {
                        localFileUploadUtils.deleteLocalFileUrl(oldWj.getFcflj());
                    }
                    if (StringUtils.isNotBlank(oldWj.getFjdcflj())) {
                        localFileUploadUtils.deleteLocalFileUrl(oldWj.getFjdcflj());
                    }
                }
            }
        }
        return Result.ok();
    }

    /**
     * ************************************* 【  审 评 文 件 初始化】 *******************************
     */

    /**
     *  初始化 - 函审通知
     */
    public Result initHstz(String sqsxid,String type,String wjmbId){

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        SpcyRsWjmb wjmb = null;
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        List<SpcySqsxXpjl> xpList = spcySqsxXpjlService.list(new LambdaQueryWrapper<SpcySqsxXpjl>()
                .eq(SpcySqsxXpjl::getFsqsxid, sqsxid)
                .eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_YES)
                .eq(SpcySqsxXpjl::getFspcylx, SpcyConstant.FLOW_TYPE_SP)
                .orderByAsc(SpcySqsxXpjl::getFzpsj));
        if (CollectionUtils.isEmpty(xpList)){
            return Result.error("请先选择【同意】选派的专家人员，再进行初始化文件。");
        }
        if (CollectionUtils.isNotEmpty(xpList)) {
            for (SpcySqsxXpjl xpjl : xpList) {
                map = getObjectMap(sqsxid,xpjl.getId());
                wjxsmc = type+"-"+xpjl.getFjcyxm(); // 文件显示名称

                // 重置-文件显示名称
                if (StringUtils.isNotBlank(fywflcode) && fywflcode.equals(SpcyConstant.YWFL_CODE_YLQX)){
                    wjxsmc = sqsx.getFsplx() + "-" + xpjl.getFjcyxm() + "的函";
                }

                // 是否存在该类型的文件
                LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(SpcySqsxWjjl::getFpid,sqsxid);
                queryWrapper.eq(SpcySqsxWjjl::getFwjlx1,type);
                queryWrapper.eq(StringUtils.isNotBlank(xpjl.getFuserid()),SpcySqsxWjjl::getFwjlx7,xpjl.getFuserid());
                List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);
                if (CollectionUtils.isNotEmpty(oldWjllist)) {
                    continue;
                }

                sjmc = GetUuIdUtils.NotReplaceUuId();
                wjccmc = sjmc + wjlx;
                cflj = newDir + "/" + wjccmc;
                newWjjl = new SpcySqsxWjjl();
                BeanUtil.copyProperties(wjjl, newWjjl);

                String wordText = genSqsxWj(cflj, wjlx, wjmbUrl, destUrl, formatType, map, true);

                this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, xpjl.getId(), xpjl.getFjcyxm(),xpjl.getFuserid());
            }
        }

        return Result.ok();
    }

    /**
     *  初始化 - 复核单位通知
     */
    public Result initYpzcFhdwtz(String sqsxid,String type,String wjmbId){

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        SpcyRsWjmb wjmb = null;
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        map = getObjectMap(sqsxid,""); // 获取替换相关内容

        String fqymc = sqsx.getFqymc();
        wjxsmc = type+"-"+fqymc; // 文件显示名称

        // 是否存在该类型的文件
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid,sqsxid);
        queryWrapper.eq(SpcySqsxWjjl::getFwjlx1,type);
        List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);

        // 不存在，新增
        if (CollectionUtils.isNotEmpty(oldWjllist)) {
            throw new BusinessException("生成失败！已经存在该材料文件了！");
        }

        sjmc = GetUuIdUtils.NotReplaceUuId();
        wjccmc = sjmc + wjlx;
        cflj = newDir + "/" + wjccmc;
        newWjjl = new SpcySqsxWjjl();
        BeanUtil.copyProperties(wjjl, newWjjl);


        String wordText = genSqsxWj(cflj, wjlx, wjmbUrl, destUrl, formatType, map, true);

        this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, sqsx.getFqyid(), fqymc);

        return Result.ok("初始化成功");
    }


    /**
     *  初始化 - 专家审评意见
     */
    public Result initZjspyj(String sqsxid,String type,String wjmbId){

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        SpcyRsWjmb wjmb = null;
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        map = getObjectMap(sqsxid,""); // 获取替换相关内容

        String fsxmc = sqsx.getFsxmc();
        wjxsmc = type + "-" + fsxmc; // 文件显示名称
        String czrid = ""; // 操作人id
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser != null){
            czrid = currentUser.getId();
            wjxsmc += "-"+currentUser.getName();
        }

        // 是否存在该类型的文件
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid,sqsxid);
        queryWrapper.eq(SpcySqsxWjjl::getFwjlx1,type);
        List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);

        // 不存在，新增
        if (CollectionUtils.isNotEmpty(oldWjllist)) {
            throw new BusinessException("生成失败！已经存在该材料文件了！");
        }

        sjmc = GetUuIdUtils.NotReplaceUuId();
        wjccmc = sjmc + wjlx;
        cflj = newDir + "/" + wjccmc;
        newWjjl = new SpcySqsxWjjl();
        BeanUtil.copyProperties(wjjl, newWjjl);

        String wordText = genSqsxWj(cflj, wjlx, wjmbUrl, destUrl, formatType, map, true);

        this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, sqsxid, wjxsmc, czrid);

        return Result.ok("初始化成功");

    }

    /**
     *  初始化 - 移送函模板
     */
    public Result initYshmb(String sqsxid,String type,String wjmbId){

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        SpcyRsWjmb wjmb = null;
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        map = getObjectMap(sqsxid,""); // 获取替换相关内容
        /*if (StringUtils.isNotBlank(fywflcode)){
            // 药品流通 模板字段
            if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPJY)){
                map.putAll(this.mbFieldByYplt(sqsxid));

            }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_HZP)){
                map.putAll(this.mdFieldXcjcblByHzp(sqsxid));
            }
        }*/

        String fqymc = sqsx.getFsxmc();
        wjxsmc = "移送函" + "-"+ fqymc; // 文件显示名称

        // 是否存在该类型的文件
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid,sqsxid);
        queryWrapper.eq(SpcySqsxWjjl::getFwjlx1,type);
        List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);

        // 不存在，新增
        if (CollectionUtils.isNotEmpty(oldWjllist)) {
            throw new BusinessException("生成失败！已经存在该材料文件了！");
        }

        sjmc = GetUuIdUtils.NotReplaceUuId();
        wjccmc = sjmc + wjlx;
        cflj = newDir + "/" + wjccmc;
        newWjjl = new SpcySqsxWjjl();
        BeanUtil.copyProperties(wjjl, newWjjl);

        String wordText = genSqsxWj(cflj, wjlx, wjmbUrl, destUrl, formatType, map, false);

        this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, sqsxid, wjxsmc);

        return Result.ok("初始化成功");
    }

    /**
     *  初始化 - 检查整改材料要求
     */
    public Result initJczgclyq(String sqsxid,String type,String wjmbId){
        // 是否存在该类型的文件
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid,sqsxid);
        queryWrapper.eq(SpcySqsxWjjl::getFwjlx1,type);
        List<SpcySqsxWjjl> oldWjllist = spcySqsxWjjlService.list(queryWrapper);
        // 不存在，新增
        if (CollectionUtils.isNotEmpty(oldWjllist)) {
            return Result.error(type+"已经存在该材料文件了！");
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在id为【"+sqsxid+"】。");
        }
        String fywflcode = sqsx.getFywflcode();

        SpcyRsWjmb wjmb = null;
        String wjmbUrl = ""; // 模板路径
        SqsxWjmbVo wjmbBySqsxId = this.getWjmbBySqsxId(wjmbId, sqsxid, sqsx, fywflcode, type);
        if (wjmbBySqsxId != null && StringUtils.isBlank(wjmbBySqsxId.getErrorMsg())){
            wjmbUrl = wjmbBySqsxId.getWjmbUrl();
            wjmb = wjmbBySqsxId.getWjmb();

        }else {
            return Result.error(wjmbBySqsxId.getErrorMsg());
        }

        String destUrl = ""; //保存路径
        String wjlx = Util.getWjHz(wjmbUrl); // 获取文件路径的 文件类型
        String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

        Map<String,String> map = new HashMap<>();
        SpcySqsxWjjl newWjjl = null;
        String wjxsmc = ""; // 文件显示名称
        String sjmc = ""; // 文件虚假名称
        String wjccmc = ""; // 文件存储名称

        String newDir = "/" + DateConversionUtils.DateToMonthTwo(new Date());
        this.initLocalTempFileMl(newDir);
        String cflj = ""; // 文件存放路径

        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setFywflcode(fywflcode);
        wjjl.setFpid(sqsxid);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjlx1(type);
        wjjl.setFwjlx2(wjmb.getId());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

        map = getObjectMap(sqsxid,""); // 获取替换相关内容

        String fqymc = sqsx.getFqymc();
        wjxsmc = type + "-" + fqymc; // 文件显示名称

        sjmc = GetUuIdUtils.NotReplaceUuId();
        wjccmc = sjmc + wjlx;
        cflj = newDir + "/" + wjccmc;
        newWjjl = new SpcySqsxWjjl();
        BeanUtil.copyProperties(wjjl, newWjjl);

        String wordText = genSqsxWj(cflj, wjlx, wjmbUrl, destUrl, formatType, map, false);

        this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, sqsx.getFqyid(), wjxsmc);

        return Result.ok("初始化成功");
    }

    /** 初始化申请事项模板文件记录 */
    public void initSqsxMbWjjl(SpcySqsxWjjl newWjjl,
                               String wjxsmc, String wjccmc,
                               String cflj, String wordText,
                               String fbusid, String fbusmc){
        this.initSqsxMbWjjl(newWjjl, wjxsmc, wjccmc, cflj, wordText, fbusid, fbusmc, "");
    }
    private void initSqsxMbWjjl(SpcySqsxWjjl newWjjl,
                                String wjxsmc, String wjccmc,
                                String cflj, String wordText,
                                String fbusid, String fbusmc, String fwjlx7){
        newWjjl.setId(GetUuIdUtils.ReplaceUuId());
        newWjjl.setFwjxsmc(wjxsmc);
        newWjjl.setFwjccmc(wjccmc);
        newWjjl.setFcflj(cflj);
        newWjjl.setFwjnr(wordText);
        newWjjl.setFbusid(fbusid);
        newWjjl.setFbusmc(fbusmc);
        newWjjl.setFwjlx7(fwjlx7);

        if (StringUtils.isNotBlank(wordText) && wordText.equalsIgnoreCase("ERROR")){
            return;
        }
        spcySqsxWjjlService.save(newWjjl);
    }

    /** 初始化申请事项文件记录 */
    public void initSqsxWjjl(String sqsxid, String type, String fwjlx, String wjxsmc, String wjccmc, String cflj){
        spcySqsxWjjlService.intiAndSaveInitSqsxWjjl(sqsxid, type, fwjlx, wjxsmc, wjccmc, cflj);
    }

    /**
     * 申请事项 - 初始化观察员
     */
    public Result initSqsxGcy(String sqsxid){
        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxInfoById(sqsxid);
        if (sqsx == null){
            return Result.error("申请事项不存在");
        }

        if (StringUtils.isNotBlank(sqsx.getFcygcyid())){
            return Result.ok("初始化成功");
        }

        String fszdq = "";
        if (StringUtils.isNotBlank(sqsx.getFszdq())){
            fszdq = SpcyCommonConstant.getQyszdsByQydz(sqsx.getFszdq());
        }else if (StringUtils.isNotBlank(sqsx.getFjcdd())){
            fszdq = SpcyCommonConstant.getQyszdsByQydz(sqsx.getFjcdd());
        }else if (StringUtils.isNotBlank(sqsx.getFqyzszcdz())){
            fszdq = SpcyCommonConstant.getQyszdsByQydz(sqsx.getFqyzszcdz());
        }

        if (StringUtils.isBlank(fszdq)){
            return Result.ok("初始化成功");
        }

        List<SpcyRsGcy> gcyList = spcyRsGcyService.list(new LambdaQueryWrapper<SpcyRsGcy>()
                .eq(SpcyRsGcy::getFszds, fszdq));

        if (CollectionUtils.isNotEmpty(gcyList)){
            SpcyRsGcy gcy = gcyList.get(0);

            LambdaUpdateWrapper<SpcySqsxBasic> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcySqsxBasic::getId, sqsxid);

            SpcySqsxBasic updateBean = new SpcySqsxBasic();
            updateBean.setFcygcyid(gcy.getId());
            updateBean.setFcygcy(gcy.getFgcyxm());
            updateBean.setFcygcybm(gcy.getFdwmc());

            spcySqsxBaiscService.update(updateBean, updateWrapper);

            String logNr = "初始化观察员：【观察员id】设置为["+gcy.getId()+"];" +
                    "【观察员姓名】设置为["+gcy.getFgcyxm()+"];【观察员部门】设置为["+gcy.getFgcyxm()+"]";
            spcyLogService.addLogs("初始化观察员",logNr,sqsxid,LogConstant.LOGS_SQSX_BASIC,SpcyConstant.LOGS_SYS_TYPE_XZ);
        }else {
            return Result.error("【"+sqsx.getFszdq()+"】观察员不存在");
        }

        return Result.ok("初始化成功");
    }


    /**
     * 获取文件模板及绝对路径
     * @param sqsx  申请事项
     * @param fywflcode 业务分类科室编码
     * @param type 文件模板类型
     * @return
     */
    public Result getWjmbByYwflcode(SpcySqsxBasic sqsx,String fywflcode, String type){
        SpcyRsWjmb wjmb = null;

        fywflcode = this.replaceYwflcode(fywflcode);

        if (StringUtils.isNotBlank(fywflcode)) {

            List<SpcyRsWjmb> wjmbList = new ArrayList<>();

//            if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPSC)){
//            List<String> typeList = StringPatternUtils.getListBySplitRegex(sqsx.getFjclx(), "、");

            /** 属于合并事项 */
            if (StringUtils.isNotBlank(sqsx.getFhbsxids())){
                List<String> typeList = StringPatternUtils.getListBySplitRegex(sqsx.getFjclx(), "、");
                LambdaQueryWrapper<SpcyRsWjmb> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(SpcyRsWjmb::getFywflcode, fywflcode);
                queryWrapper.eq(SpcyRsWjmb::getFmblx1, type);

                queryWrapper.and(
                        q->q.or(
                                q2->{
                                    for (String key : typeList){
                                        q2.like(SpcyRsWjmb::getFmblx2, key);
                                    }
                                }
                        )
                );


                queryWrapper.isNotNull(SpcyRsWjmb::getFcflj);
                queryWrapper.orderByDesc(SpcyRsWjmb::getUpdateTime);

                wjmbList = spcyRsWjmbService.list(queryWrapper);
            }else {

                wjmbList = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                        .eq(SpcyRsWjmb::getFywflcode, fywflcode)
                        .eq(SpcyRsWjmb::getFmblx1, type)
                        .and(q -> q.like(StringUtils.isNotBlank(sqsx.getFjclx()), SpcyRsWjmb::getFmblx2, sqsx.getFjclx())
                                .or()
                                .like(StringUtils.isNotBlank(sqsx.getFsxmc()), SpcyRsWjmb::getFmblx2, sqsx.getFsxmc())
                        )
                        .isNotNull(SpcyRsWjmb::getFcflj)
                        .orderByDesc(SpcyRsWjmb::getUpdateTime));
            }

            /*else {
                wjmbList = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                        .eq(SpcyRsWjmb::getFywflcode, fywflcode)
                        .eq(SpcyRsWjmb::getFmblx1, type)
                        .like(StringUtils.isNotBlank(basic.getFsxmc()), SpcyRsWjmb::getFmblx2, basic.getFsxmc())
                        .isNotNull(SpcyRsWjmb::getFcflj)
                        .orderByDesc(SpcyRsWjmb::getUpdateTime));
            }*/

            if (CollectionUtils.isEmpty(wjmbList)) {
                wjmbList = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                        .eq(SpcyRsWjmb::getFywflcode, fywflcode)
                        .eq(SpcyRsWjmb::getFmblx1, type)
                        .isNotNull(SpcyRsWjmb::getFcflj)
                        .orderByDesc(SpcyRsWjmb::getUpdateTime));
                if (CollectionUtils.isNotEmpty(wjmbList)) {
                    wjmb = wjmbList.get(0);
                } else {
                    String fywflname = sqsx.getFywflname();
                    return Result.error("业务【" + fywflname + "】和类型【" + type + "】文件模板数据不存在。");
                }
            } else {
                wjmb = wjmbList.get(0);
            }
        }

        return Result.ok(wjmb);
    }

    /**
     * 获取当前申请事项-对应类型的文件模板
     * @param sqsxid 申请事项id
     * @param type 文件模板类型
     * @return 返回对应文件记录中选用的当前文件模板id
     */
    public String getSqsxTypeFile(String sqsxid, String type){
        if (StringUtils.isBlank(sqsxid)){
            return "";
        }
        if (StringUtils.isBlank(type)){
            return "";
        }
        /*List<SpcySqsxWjjl> list = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                .eq(SpcySqsxWjjl::getFpid, sqsxid)
                .eq(SpcySqsxWjjl::getFwjlx1, type));*/
        List<String> list = spcySqsxWjjlService.findFwjlx2ByFpidAndFwjlx1(sqsxid, type);
        if (CollectionUtil.isNotEmpty(list)){
            for (String fwjlx2 : list) {
                // 返回对应文件记录中选用的当前文件模板id
                if (StringUtils.isNotBlank(fwjlx2)) {
                    return fwjlx2;
                }
            }
        }

        return "";
    }

    /** -------------------------------------------【 核查员选派 - 功能 】--------------------------------------------------------- */

    /**
     * 重新自动选派人员
     * @param nowXpjl
     */
    public void xpHcyByAgain(SpcySqsxXpjl nowXpjl){
        String sqsxid = nowXpjl.getFsqsxid();
        String fywflcode = nowXpjl.getFywflcode();
        SpcySqsxBasic sqsx = this.getSqsxBySqsxId(sqsxid); // 获取申请事项

        String fssqy = "";
        String zytc = "";
        String hbqy = "";
        String kssj = "";
        String jssj = "";
        String ssk = "";
        String fusertype = "";

        // 查验
        if (org.apache.commons.lang3.StringUtils.isNotBlank(nowXpjl.getFspcylx()) && nowXpjl.getFspcylx().equals(SpcyConstant.FLOW_TYPE_CY)){
            fusertype = SpcyConstant.HCY_USERTYPE_JCY;

            if (sqsx != null){
                if (sqsx.getFcykssj() != null) {
                    kssj = DateConversionUtils.DateToStringYYYYMMDD(sqsx.getFcykssj());
                }
                if (sqsx.getFcyjssj() != null) {
                    jssj = DateConversionUtils.DateToStringYYYYMMDD(sqsx.getFcyjssj());
                }
            }

        }else if (org.apache.commons.lang3.StringUtils.isNotBlank(nowXpjl.getFspcylx()) && nowXpjl.getFspcylx().equals(SpcyConstant.FLOW_TYPE_SP)){
            // 审评
            fusertype = SpcyConstant.HCY_USERTYPE_ZJ;

            if (sqsx != null){
                if (sqsx.getFspkssj() != null) {
                    kssj = DateConversionUtils.DateToStringYYYYMMDD(sqsx.getFspkssj());
                }
                if (sqsx.getFspjssj() != null) {
                    jssj = DateConversionUtils.DateToStringYYYYMMDD(sqsx.getFspjssj());
                }
            }
        }

        // 判断所属库
        if (org.apache.commons.lang3.StringUtils.isNotBlank(nowXpjl.getFssk())){
            if (nowXpjl.getFssk().equals(SpcyConstant.HCY_SSK_ZZ)){
                ssk = SpcyConstant.HCY_SSK_ZZ;
            }else if (nowXpjl.getFssk().equals(SpcyConstant.HCY_SSK_ZY)){
                ssk = SpcyConstant.HCY_SSK_ZY;
            }else if (nowXpjl.getFssk().equals(SpcyConstant.HCY_SSK_YB)){
                ssk = SpcyConstant.HCY_SSK_YB;
            }
        }else {
            ssk = SpcyConstant.HCY_SSK_ZY;
        }

        Integer fzyrs = 1;
        Integer fzzrs = 1;
        Integer fybrs = 1;
        Result<?> result = this.xpHcyByAuto(new HcyXpVo(),sqsxid, null,ssk, fusertype, zytc, hbqy, fssqy, fywflcode, kssj, jssj, fzzrs, fzyrs,fybrs);
        if (!result.isSuccess()){
            log.error("自动重新选派人员出错：{}",result.getMessage());
        }
    }

    /**
     * 方案制作环节 - 自动选派检查人员
     * @param sqsxId 申请事项id
     * @param ssk 所属库
     * @param fssqy 选择区域(所属区域)
     * @param fusertype 人员分类（检查员、专家）
     * @param hbqy 回避区域
     * @param ywflCode 业务分类编码
     * @param zytc 专业特长
     * @param jssj 开始时间
     * @param kssj 结束时间
     */
    public Result xpHcyByAuto(HcyXpVo hcyXpVo,String sqsxId,SpcySqsxBasic basic,String ssk,String fusertype, String zytc,
                              String hbqy,String fssqy,String ywflCode, String kssj,String jssj,Integer fzzrs,Integer fzyrs,Integer fybrs){
        if (basic == null && StringUtils.isNotBlank(sqsxId)) {
            basic = spcySqsxBaiscService.selectSqsxInfoById(sqsxId);
            if (basic == null) {
                throw new BusinessException("当前申请事项数据不存在，请联系管理员！");
            }
        }
        if (basic.getFcykssj() != null && basic.getFcyjssj() != null){
            kssj = DateConversionUtils.DateToStringYYYYMMDD(basic.getFcykssj());
            jssj = DateConversionUtils.DateToStringYYYYMMDD(basic.getFcyjssj());
        }

        Integer zzrs = 1; // 随机人数
        if (StringUtils.isNotBlank(ssk) && ssk.equals(SpcyConstant.HCY_SSK_ZZ)) {
            zzrs = fzzrs;
        }else if (StringUtils.isNotBlank(ssk) && ssk.equals(SpcyConstant.HCY_SSK_ZY)){
            zzrs = fzyrs;
        }else if (StringUtils.isNotBlank(ssk) && ssk.contains(SpcyConstant.HCY_SSK_YBK)){
            zzrs = fybrs;
        }

        if (StringUtils.isBlank(ywflCode)){
            ywflCode = basic.getFywflcode();
        }
        String jcylx = hcyXpVo.getFlx(); // 检查员类别
        if (StringUtils.isBlank(jcylx) && StringUtils.isNotBlank(ywflCode)){
            jcylx = JcydjbConstant.getJcylxByYwflcode(ywflCode);
        }

        List<RsHcyVo> spcyRsHcyList = new ArrayList<>();
        RsHcyVo rsHcyVo = new RsHcyVo();
        // 存在回避区域
        if (StringUtils.isNotEmpty(hbqy)){
            rsHcyVo.setHbqy(hbqy);
            rsHcyVo.setZytc(zytc);
//            rsHcyVo.setFywflcode(ywflCode); // 业务分类
            rsHcyVo.setFlx(jcylx); // 检查员类别
            rsHcyVo.setFssk(ssk); // 所属库
            rsHcyVo.setFstate(SpcyCommonConstant.JCY_STATE_YX); // 有效
            rsHcyVo.setStatus(SysUserConstant.STATUS_YES); // 账户正常
            rsHcyVo.setFgzzt(SpcyConstant.JCY_ZT_ZG); // 在岗
            rsHcyVo.setFusertype(fusertype);

            // 请假范围查询
            if (DateConversionUtils.isValidDateStringYmd(kssj) && DateConversionUtils.isValidDateStringYmd(jssj)){
                rsHcyVo.setQjfwcx("是");
                rsHcyVo.setKssj(DateConversionUtils.convertStringToDateYmd(kssj));
                rsHcyVo.setJssj(DateConversionUtils.convertStringToDateYmd(jssj));
            }

            spcyRsHcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

            if (CollectionUtils.isEmpty(spcyRsHcyList)) {
                String msg = "";
                if (StringUtils.isBlank(zytc)){
                    msg += "在回避区域【" + hbqy + "】中,";
                }else {
                    msg += "在回避区域【" + hbqy + "】和专业特长【" + zytc + "】中,";
                }

                if (StringUtils.isNotBlank(ssk)){
                    msg += "没有符合[ "+ssk+" ]的人员选派！";
                }else {
                    msg += "没有符合的人员选派!";
                }

                return Result.error(msg);
            }
        }else {
            rsHcyVo.setFssqy(fssqy);
            rsHcyVo.setZytc(zytc);
//            rsHcyVo.setFywflcode(ywflCode); // 业务分类
            rsHcyVo.setFlx(jcylx); // 检查员类别
            rsHcyVo.setFssk(ssk); // 所属库
            rsHcyVo.setFstate(SpcyCommonConstant.JCY_STATE_YX); // 有效
            rsHcyVo.setStatus(SysUserConstant.STATUS_YES); // 账户正常
            rsHcyVo.setFgzzt(SpcyConstant.JCY_ZT_ZG); // 在岗
            rsHcyVo.setFusertype(fusertype);

            // 请假范围查询
            if (DateConversionUtils.isValidDateStringYmd(kssj) && DateConversionUtils.isValidDateStringYmd(jssj)){
                rsHcyVo.setQjfwcx("是");
                rsHcyVo.setKssj(DateConversionUtils.convertStringToDateYmd(kssj));
                rsHcyVo.setJssj(DateConversionUtils.convertStringToDateYmd(jssj));
            }

            spcyRsHcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

            if (CollectionUtils.isEmpty(spcyRsHcyList)){
                String msg = "";
                if (StringUtils.isBlank(zytc)){
                    msg += "在选择区域【" + fssqy + "】中,";
                }else {
                    msg += "在选择区域【" + fssqy + "】和专业特长【" + zytc + "】中,";
                }

                if (StringUtils.isNotBlank(ssk)){
                    msg += "没有符合[ "+ssk+" ]的人员选派！";
                }else {
                    msg += "没有符合的人员选派!";
                }

                return Result.error(msg);
            }
        }

        // 随机选择人员
        List<RsHcyVo> tSpcyInspectors = RandomListUtils.randomList(spcyRsHcyList, zzrs);

        // 不为空人员
        if (CollectionUtils.isNotEmpty(tSpcyInspectors)) {
            Result result = this.initXpHcy(basic, tSpcyInspectors, sqsxId, fusertype, SpcyConstant.XPFS_ZDXZ,kssj, jssj, hcyXpVo.getFdsxpywid());
            if (!result.isSuccess()){
                return Result.error(result.getMessage());
            }
        }else {
            return Result.error("随机人员为空，选派失败");
        }
        return Result.ok("选派成功");
    }

    /**
     * 方案制作环节 - 自动选派专家人员
     * @param sqsxId 申请事项id
     * @param ssk 所属库
     * @param fssqy 选择区域(所属区域)
     * @param fusertype 人员分类（检查员、专家）
     * @param hbqy 回避区域
     * @param ywflCode 业务分类编码
     * @param zytc 专业特长
     * @param jssj 开始时间
     * @param kssj 结束时间
     */
    public Result xpZjByAutoInCommon(String sqsxId,SpcySqsxBasic basic,String ssk,String fusertype, String zytc,
                                     String hbqy,String fssqy,String ywflCode, String kssj,String jssj,
                                     Integer fzzrs,Integer fzyrs,Integer fybrs,Integer fzjrs){
        if (basic == null && StringUtils.isNotBlank(sqsxId)) {
            basic = spcySqsxBaiscService.selectSqsxInfoById(sqsxId);
            if (basic == null) {
                throw new BusinessException("当前申请事项数据不存在，请联系管理员！");
            }
        }

        Integer zzrs = 1; // 随机人数
        if (StringUtils.isNotBlank(ssk)) {
            if (StringUtils.isNotBlank(ssk) && ssk.equals(SpcyConstant.HCY_SSK_ZZ)) {
                zzrs = fzzrs;
                ssk = SpcyConstant.HCY_SSK_ZZ;

            } else if (StringUtils.isNotBlank(ssk) && ssk.equals(SpcyConstant.HCY_SSK_ZY)) {
                zzrs = fzyrs;
                ssk = SpcyConstant.HCY_SSK_ZY;
            }

            if (fzjrs != null && fzjrs > 0) {
                zzrs = fzjrs;
            }

        }else {
            if (fzjrs != null && fzjrs > 0){
                zzrs = fzjrs;
            }else {
                if (fzzrs != null && fzzrs > 0) {
                    zzrs = fzzrs;
                } else if (fzyrs != null && fzyrs > 0) {
                    zzrs = fzyrs;
                } else if (fybrs != null && fybrs > 0) {
                    zzrs = fybrs;
                } else {
                    zzrs = 1;
                }
            }
        }

        if (StringUtils.isBlank(ywflCode)){
            ywflCode = basic.getFywflcode();
        }

        if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPZCLC)){
            ywflCode = SpcyConstant.YWFL_CODE_YPZC;
        }

        List<RsHcyVo> spcyRsHcyList = new ArrayList<>();
        RsHcyVo rsHcyVo = new RsHcyVo();
        // 存在回避区域
        if (StringUtils.isNotEmpty(hbqy)){
//            rsHcyVo.setHbqy(hbqy);
            rsHcyVo.setZytc(zytc);
            rsHcyVo.setFywflcode(ywflCode); // 业务分类
            rsHcyVo.setFssk(ssk); // 所属库
            rsHcyVo.setFstate(SpcyCommonConstant.JCY_STATE_YX); // 有效
            rsHcyVo.setStatus(SysUserConstant.STATUS_YES); // 账户正常
            rsHcyVo.setFgzzt(SpcyConstant.JCY_ZT_ZG); // 在岗
            rsHcyVo.setFusertype(fusertype);

            spcyRsHcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

            if (CollectionUtils.isEmpty(spcyRsHcyList)) {
                String msg = "";
                if (StringUtils.isNotBlank(zytc)) {
                    msg += "在专业特长【" + zytc + "】中,";
                }
                msg += "没有符合的专家人员选派!";
                return Result.error(msg);
            }
        }else {
//            rsHcyVo.setFssqy(fssqy);
            rsHcyVo.setZytc(zytc);
            rsHcyVo.setFywflcode(ywflCode); // 业务分类
            rsHcyVo.setFssk(ssk); // 所属库
            rsHcyVo.setFstate(SpcyCommonConstant.JCY_STATE_YX); // 有效
            rsHcyVo.setStatus(SysUserConstant.STATUS_YES); // 账户正常
            rsHcyVo.setFgzzt(SpcyConstant.JCY_ZT_ZG); // 在岗
            rsHcyVo.setFusertype(fusertype);

            spcyRsHcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

            if (CollectionUtils.isEmpty(spcyRsHcyList)){
                String msg = "";
                if (StringUtils.isNotBlank(zytc)) {
                    msg += "在专业特长【" + zytc + "】中,";
                }
                msg += "没有符合的专家人员选派!";
                return Result.error(msg);
            }
        }

        // 随机选择人员
        List<RsHcyVo> tSpcyInspectors = RandomListUtils.randomList(spcyRsHcyList, zzrs);

        // 不为空人员
        if (CollectionUtils.isNotEmpty(tSpcyInspectors)) {
            Result result = this.initXpHcy(basic, tSpcyInspectors, sqsxId, fusertype, SpcyConstant.XPFS_ZDXZ,kssj, jssj, "");
            if (!result.isSuccess()){
                return Result.error(result.getMessage());
            }
        }else {
            return Result.error("随机人员为空，选派失败");
        }
        return Result.ok("选派成功");
    }

    /**
     * 方案制作环节 - 自动选派人员  (药品生产)
     * @param sqsxId 申请事项id
     * @param ssk 所属库
     * @param fssqy 选择区域(所属区域)
     * @param fusertype 人员分类（检查员、专家）
     * @param hbqy 回避区域
     * @param ywflCode 业务分类编码
     * @param zytc 专业特长
     * @param jssj 开始时间
     * @param kssj 结束时间
     * @param fxptjid 选派条件id
     * @param xpfs 选派方式
     */
    public Result xpHcyByAutoInYpsc(HcyXpVo hcyXpVo,String sqsxId,SpcySqsxBasic basic,String ssk,String fusertype, String zytc,
                                    String hbqy,String fssqy,String ywflCode, String kssj,String jssj,Integer fzzrs,Integer fzyrs,Integer fybrs,String fxptjid,String xpfs){

        if (basic == null && StringUtils.isNotBlank(sqsxId)) {
            basic = spcySqsxBaiscService.selectSqsxInfoById(sqsxId);
            if (basic == null) {
                throw new BusinessException("当前申请事项数据不存在，请联系管理员！");
            }
        }

        if (basic.getFcykssj() != null && basic.getFcyjssj() != null){
            kssj = DateConversionUtils.DateToStringYYYYMMDD(basic.getFcykssj());
            jssj = DateConversionUtils.DateToStringYYYYMMDD(basic.getFcyjssj());
        }

        Integer sjrs = 1; // 随机人数
        /*int sjssk = 0; // 0未知/1：组长/2: 组员/3：预备
        int sjxzrs = 0; // 随机选中人数*/

        if (StringUtils.isNotBlank(ssk)){
            if (ssk.equals(SpcyConstant.HCY_SSK_ZZ)) {
                sjrs = fzzrs;
//                sjssk = 1;

            }else if (ssk.equals(SpcyConstant.HCY_SSK_ZY)){
                sjrs = fzyrs;
//                sjssk = 2;

            }else if (ssk.contains(SpcyConstant.HCY_SSK_YBK)){
                sjrs = fybrs;
//                sjssk = 3;
            }
        }

        if (StringUtils.isBlank(ywflCode)){
            ywflCode = basic.getFywflcode();
        }

        String jcylx = hcyXpVo.getFlx(); // 检查员类别
        if (StringUtils.isBlank(jcylx) && StringUtils.isNotBlank(ywflCode)){
            jcylx = JcydjbConstant.getJcylxByYwflcode(ywflCode);
        }

        // 药品生产 - 需要 在 随机选择人数大于等于2 ，需要选择一个药检所的人
        if (StringUtils.isNotBlank(ywflCode) && ywflCode.equals(SpcyConstant.YWFL_CODE_YPSC)){
            if (sjrs >= 2){
                List<RsHcyVo> spcyRsHcyList1 = new ArrayList<>();
                RsHcyVo yjsHcy = new RsHcyVo();
                yjsHcy.setZytc(zytc);
                yjsHcy.setFlx(jcylx);
                yjsHcy.setFssk(ssk); // 所属库
                yjsHcy.setFstate(SpcyCommonConstant.JCY_STATE_YX); // 有效
                yjsHcy.setStatus(SysUserConstant.STATUS_YES); // 账户正常
                yjsHcy.setFgzzt(SpcyConstant.JCY_ZT_ZG); // 在岗
                yjsHcy.setFusertype(fusertype);
                yjsHcy.setFsfyjsry(SpcyConstant.HCY_SFYJSRY_YES); // 为药检所人员
                yjsHcy.setCxxpjlsqsxid(sqsxId);

                if (StringUtils.isNotBlank(kssj) && StringUtils.isNotBlank(jssj)) {
                    yjsHcy.setSfcxxpqj("是");
                    yjsHcy.setCxkssj(kssj);
                    yjsHcy.setCxjssj(jssj);
                }

                // 存在回避区域
                if (StringUtils.isNotEmpty(hbqy)){
                    yjsHcy.setHbqy(hbqy);
                }else {
                    yjsHcy.setFssqy(fssqy);
                }

                spcyRsHcyList1 = spcyRsHcyService.getRsHcyList(yjsHcy);
                if (CollectionUtils.isNotEmpty(spcyRsHcyList1)) {
                    // 随机选择一个 药检所的人
                    Result result = this.xpHcyByYjs(basic, sqsxId, fusertype, kssj, jssj, spcyRsHcyList1, hcyXpVo.getFdsxpywid());
                    if (result.isSuccess()) {
                        sjrs = sjrs - 1; // 减少一个选人数量
                        /*sjxzrs = sjxzrs + 1;

                         *//** 更新选派条件选中人数 *//*
                        spcySqsxXptjjlService.updateXptjXzrsById(fxptjid, sjssk, 1);*/

                    }
                }
            }
        }

        List<RsHcyVo> spcyRsHcyList = new ArrayList<>();
        RsHcyVo rsHcyVo = new RsHcyVo();
        rsHcyVo.setZytc(zytc);
        rsHcyVo.setFlx(jcylx);
        rsHcyVo.setFssk(ssk); // 所属库
        rsHcyVo.setFstate(SpcyCommonConstant.JCY_STATE_YX); // 有效
        rsHcyVo.setStatus(SysUserConstant.STATUS_YES); // 账户正常
        rsHcyVo.setFgzzt(SpcyConstant.JCY_ZT_ZG); // 在岗
        rsHcyVo.setFusertype(fusertype);
        rsHcyVo.setCxxpjlsqsxid(sqsxId);

        if (StringUtils.isNotBlank(kssj) && StringUtils.isNotBlank(jssj)) {
            rsHcyVo.setSfcxxpqj("是");
            rsHcyVo.setCxkssj(kssj);
            rsHcyVo.setCxjssj(jssj);
        }

        // 存在回避区域
        if (StringUtils.isNotEmpty(hbqy)){
            rsHcyVo.setHbqy(hbqy);

            spcyRsHcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

            if (CollectionUtils.isEmpty(spcyRsHcyList)) {

                String msg = "";
                if (StringUtils.isBlank(zytc)){
                    if (StringUtils.isNotBlank(hbqy)) {
                        msg += "在回避区域【" + hbqy + "】中,";
                    }
                }else {
                    if (StringUtils.isNotBlank(hbqy)) {
                        msg += "在回避区域【" + hbqy + "】和专业特长【" + zytc + "】中,";
                    }else {
                        msg += "在专业特长【" + zytc + "】中,";
                    }
                }

                if (StringUtils.isNotBlank(ssk)){
                    msg += "没有符合[ "+ssk+" ]的人员选派！";
                }else {
                    msg += "没有符合的人员选派!";
                }

                return Result.error(msg);
            }
        }else {
            rsHcyVo.setFssqy(fssqy);

            spcyRsHcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

            if (CollectionUtils.isEmpty(spcyRsHcyList)){

                String msg = "";
                if (StringUtils.isBlank(zytc)){
                    if (StringUtils.isNotBlank(fssqy)) {
                        msg += "在选择区域【" + fssqy + "】中,";
                    }
                }else {
                    if (StringUtils.isNotBlank(fssqy)) {
                        msg += "在选择区域【" + fssqy + "】和专业特长【" + zytc + "】中,";
                    }else {
                        msg += "在专业特长【" + zytc + "】中,";
                    }
                }

                if (StringUtils.isNotBlank(ssk)){
                    msg += "没有符合[ "+ssk+" ]的人员选派！";
                }else {
                    msg += "没有符合的人员选派!";
                }

                return Result.error(msg);
            }
        }

        // 随机选择人员
        List<RsHcyVo> tSpcyInspectors = RandomListUtils.randomList(spcyRsHcyList, sjrs);

        // 不为空人员
        if (CollectionUtils.isNotEmpty(tSpcyInspectors)) {
            Result result = this.initXpHcy(basic, tSpcyInspectors, sqsxId, fusertype, xpfs, kssj, jssj, hcyXpVo.getFdsxpywid(), fxptjid);
            if (!result.isSuccess()){
                return Result.error(result.getMessage());
            }

            /** 更新选派条件选中人数 */
            /*sjxzrs = sjxzrs + sjrs;
            spcySqsxXptjjlService.updateXptjXzrsById(fxptjid, sjssk, sjxzrs);*/

        }else {
            return Result.error("随机人员为空，选派失败");
        }

        return Result.ok("选派成功");
    }

    /**
     * 方案制作环节 - 自动选派检查人员 (药品流通)
     * @param sqsxId 申请事项id
     * @param ssk 所属库
     * @param fssqy 选择区域(所属区域)
     * @param fusertype 人员分类（检查员、专家）
     * @param hbqy 回避区域
     * @param ywflCode 业务分类编码
     * @param zytc 专业特长
     * @param jssj 开始时间
     * @param kssj 结束时间
     */
    public Result xpHcyByAutoInYplt(HcyXpVo hcyXpVo,String sqsxId,SpcySqsxBasic basic,String ssk,String fusertype, String zytc,
                                    String hbqy,String fssqy,String ywflCode, String kssj,String jssj,Integer fzzrs,Integer fzyrs,Integer fybrs){

        if (basic == null && StringUtils.isNotBlank(sqsxId)) {
            basic = spcySqsxBaiscService.selectSqsxInfoById(sqsxId);
            if (basic == null) {
                throw new BusinessException("当前申请事项数据不存在，请联系管理员！");
            }
        }
        if (basic.getFcykssj() != null && basic.getFcyjssj() != null){
            kssj = DateConversionUtils.DateToStringYYYYMMDD(basic.getFcykssj());
            jssj = DateConversionUtils.DateToStringYYYYMMDD(basic.getFcyjssj());
        }

        Integer zzrs = 1; // 随机人数
        if (StringUtils.isNotBlank(ssk) && ssk.equals(SpcyConstant.HCY_SSK_ZZ)) {
            zzrs = fzzrs;
        }else if (StringUtils.isNotBlank(ssk) && ssk.equals(SpcyConstant.HCY_SSK_ZY)){
            zzrs = fzyrs;
        }else if (StringUtils.isNotBlank(ssk) && ssk.contains(SpcyConstant.HCY_SSK_YBK)){
            zzrs = fybrs;
        }

        if (StringUtils.isBlank(ywflCode)){
            ywflCode = basic.getFywflcode();
        }

        String jcylx = hcyXpVo.getFlx(); // 检查员类别
        if (StringUtils.isBlank(jcylx) && StringUtils.isNotBlank(ywflCode)){
            jcylx = JcydjbConstant.getJcylxByYwflcode(ywflCode);
        }

        List<RsHcyVo> spcyRsHcyList = new ArrayList<>();
        RsHcyVo rsHcyVo = new RsHcyVo();
        rsHcyVo.setZytc(zytc);
        rsHcyVo.setFlx(jcylx);
        rsHcyVo.setFssk(ssk); // 所属库
        rsHcyVo.setFstate(SpcyCommonConstant.JCY_STATE_YX); // 有效
        rsHcyVo.setStatus(SysUserConstant.STATUS_YES); // 账户正常
        rsHcyVo.setFgzzt(SpcyConstant.JCY_ZT_ZG); // 在岗
        rsHcyVo.setFusertype(fusertype);
        rsHcyVo.setCxxpjlsqsxid(sqsxId);

        // 存在回避区域
        if (StringUtils.isNotEmpty(hbqy)){
            rsHcyVo.setHbqy(hbqy);

            spcyRsHcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

            if (CollectionUtils.isEmpty(spcyRsHcyList)) {
                String msg = "";
                if (StringUtils.isBlank(zytc)){
                    if (StringUtils.isNotBlank(hbqy)) {
                        msg += "在回避区域【" + hbqy + "】中,";
                    }
                }else {
                    if (StringUtils.isNotBlank(hbqy)) {
                        msg += "在回避区域【" + hbqy + "】和专业特长【" + zytc + "】中,";
                    }else {
                        msg += "在专业特长【" + zytc + "】中,";
                    }
                }

                if (StringUtils.isNotBlank(ssk)){
                    msg += "没有符合[ "+ssk+" ]的人员选派！";
                }else {
                    msg += "没有符合的人员选派!";
                }

                return Result.error(msg);
            }
        }else {
            rsHcyVo.setFssqy(fssqy);

            spcyRsHcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

            if (CollectionUtils.isEmpty(spcyRsHcyList)){
                String msg = "";
                if (StringUtils.isBlank(zytc)){
                    if (StringUtils.isNotBlank(fssqy)) {
                        msg += "在选择区域【" + fssqy + "】中,";
                    }
                }else {
                    if (StringUtils.isNotBlank(fssqy)) {
                        msg += "在选择区域【" + fssqy + "】和专业特长【" + zytc + "】中,";
                    }else {
                        msg += "在专业特长【" + zytc + "】中,";
                    }
                }

                if (StringUtils.isNotBlank(ssk)){
                    msg += "没有符合[ "+ssk+" ]的人员选派！";
                }else {
                    msg += "没有符合的人员选派!";
                }

                return Result.error(msg);
            }
        }

        // 随机选择人员
        List<RsHcyVo> tSpcyInspectors = RandomListUtils.randomList(spcyRsHcyList, zzrs);

        // 不为空人员
        if (CollectionUtils.isNotEmpty(tSpcyInspectors)) {
            Result result = this.initXpHcy(basic, tSpcyInspectors, sqsxId, fusertype, SpcyConstant.XPFS_ZDXZ,kssj, jssj, hcyXpVo.getFdsxpywid());
            if (!result.isSuccess()){
                return Result.error(result.getMessage());
            }
        }else {
            return Result.error("随机人员为空，选派失败");
        }
        return Result.ok("选派成功");
    }

    /**
     * 方案制作环节 - 自动选派检查人员 (药品注册)
     * @param sqsxId 申请事项id
     * @param ssk 所属库
     * @param fssqy 选择区域(所属区域)
     * @param fusertype 人员分类（检查员、专家）
     * @param hbqy 回避区域
     * @param ywflCode 业务分类编码
     * @param zytc 专业特长
     * @param jssj 开始时间
     * @param kssj 结束时间
     */
    public Result xpHcyByAutoInYpzc(HcyXpVo hcyXpVo,String sqsxId,SpcySqsxBasic basic,String ssk,String fusertype, String zytc,
                                    String hbqy,String fssqy,String ywflCode, String kssj,String jssj,Integer fzzrs,Integer fzyrs,Integer fybrs){
        if (basic == null && StringUtils.isNotBlank(sqsxId)) {
            basic = spcySqsxBaiscService.selectSqsxInfoById(sqsxId);
            if (basic == null) {
                throw new BusinessException("当前申请事项数据不存在，请联系管理员！");
            }
        }
        if (basic.getFcykssj() != null && basic.getFcyjssj() != null){
            kssj = DateConversionUtils.DateToStringYYYYMMDD(basic.getFcykssj());
            jssj = DateConversionUtils.DateToStringYYYYMMDD(basic.getFcyjssj());
        }

        Integer zzrs = 1; // 随机人数
        if (StringUtils.isNotBlank(ssk) && ssk.equals(SpcyConstant.HCY_SSK_ZZ)) {
            zzrs = fzzrs;
        }else if (StringUtils.isNotBlank(ssk) && ssk.equals(SpcyConstant.HCY_SSK_ZY)){
            zzrs = fzyrs;
        }else if (StringUtils.isNotBlank(ssk) && ssk.contains(SpcyConstant.HCY_SSK_YBK)){
            zzrs = fybrs;
        }

        if (StringUtils.isBlank(ywflCode)){
            ywflCode = basic.getFywflcode();
        }
        String jcylx = hcyXpVo.getFlx(); // 检查员类别
        if (StringUtils.isBlank(jcylx) && StringUtils.isNotBlank(ywflCode)){
            jcylx = JcydjbConstant.getJcylxByYwflcode(ywflCode);
        }

        List<RsHcyVo> spcyRsHcyList = new ArrayList<>();
        RsHcyVo rsHcyVo = new RsHcyVo();

        rsHcyVo.setZytc(zytc);
        rsHcyVo.setFlx(jcylx); // 检查员类别
        rsHcyVo.setFssk(ssk); // 所属库
        rsHcyVo.setFstate(SpcyCommonConstant.JCY_STATE_YX); // 有效
        rsHcyVo.setStatus(SysUserConstant.STATUS_YES); // 账户正常
        rsHcyVo.setFgzzt(SpcyConstant.JCY_ZT_ZG); // 在岗
        rsHcyVo.setFusertype(SpcyConstant.HCY_USERTYPE_JCY);
        rsHcyVo.setCxxpjlsqsxid(sqsxId);

        /** 药品注册-检查员-在岗+检查日期范围 查询 */
            /*if (DateConversionUtils.isValidDateStringYmd(kssj)){
                rsHcyVo.setFgzzt(null);
                rsHcyVo.setFwcx("是");
                rsHcyVo.setKssj(DateConversionUtils.convertStringToDateYmd(kssj));
            }*/

        // 存在回避区域
        if (StringUtils.isNotEmpty(hbqy)){
            rsHcyVo.setHbqy(hbqy);

            spcyRsHcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

            if (CollectionUtils.isEmpty(spcyRsHcyList)) {
                String msg = "";
                if (StringUtils.isBlank(zytc)){
                    if (StringUtils.isNotBlank(hbqy)) {
                        msg += "在回避区域【" + hbqy + "】中,";
                    }
                }else {
                    if (StringUtils.isNotBlank(hbqy)) {
                        msg += "在回避区域【" + hbqy + "】和专业特长【" + zytc + "】中,";
                    }else {
                        msg += "在专业特长【" + zytc + "】中,";
                    }
                }

                if (StringUtils.isNotBlank(ssk)){
                    msg += "没有符合[ "+ssk+" ]的人员选派！";
                }else {
                    msg += "没有符合的人员选派!";
                }

                return Result.error(msg);
            }
        }else {
            rsHcyVo.setFssqy(fssqy);
            spcyRsHcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

            if (CollectionUtils.isEmpty(spcyRsHcyList)){
                String msg = "";
                if (StringUtils.isBlank(zytc)){
                    if (StringUtils.isNotBlank(fssqy)) {
                        msg += "在选择区域【" + fssqy + "】中,";
                    }
                }else {
                    if (StringUtils.isNotBlank(fssqy)) {
                        msg += "在选择区域【" + fssqy + "】和专业特长【" + zytc + "】中,";
                    }else {
                        msg += "在专业特长【" + zytc + "】中,";
                    }
                }

                if (StringUtils.isNotBlank(ssk)){
                    msg += "没有符合[ "+ssk+" ]的人员选派！";
                }else {
                    msg += "没有符合的人员选派!";
                }

                return Result.error(msg);
            }
        }

        // 随机选择人员
        List<RsHcyVo> tSpcyInspectors = RandomListUtils.randomList(spcyRsHcyList, zzrs);

        // 不为空人员
        if (CollectionUtils.isNotEmpty(tSpcyInspectors)) {
            Result result = this.initXpHcy(basic, tSpcyInspectors, sqsxId, fusertype, SpcyConstant.XPFS_ZDXZ,kssj, jssj, hcyXpVo.getFdsxpywid());
            if (!result.isSuccess()){
                return Result.error(result.getMessage());
            }
        }else {
            return Result.error("随机人员为空，选派失败");
        }
        return Result.ok("选派成功");
    }

    /**
     * 方案制作环节 - 自动选派检查人员 (药品注册-临床)
     * @param sqsxId 申请事项id
     * @param ssk 所属库
     * @param fssqy 选择区域(所属区域)
     * @param fusertype 人员分类（检查员、专家）
     * @param hbqy 回避区域
     * @param ywflCode 业务分类编码
     * @param zytc 专业特长
     * @param jssj 开始时间
     * @param kssj 结束时间
     */
    public Result xpHcyByAutoInYpzcLc(HcyXpVo hcyXpVo,String sqsxId,SpcySqsxBasic basic,String ssk,String fusertype, String zytc,
                                      String hbqy,String fssqy,String ywflCode, String kssj,String jssj,Integer fzzrs,Integer fzyrs,Integer fybrs){
        if (basic == null && StringUtils.isNotBlank(sqsxId)) {
            basic = spcySqsxBaiscService.selectSqsxInfoById(sqsxId);
            if (basic == null) {
                throw new BusinessException("当前申请事项数据不存在，请联系管理员！");
            }
        }
        if (basic.getFcykssj() != null && basic.getFcyjssj() != null){
            kssj = DateConversionUtils.DateToStringYYYYMMDD(basic.getFcykssj());
            jssj = DateConversionUtils.DateToStringYYYYMMDD(basic.getFcyjssj());
        }

        Integer zzrs = 1; // 随机人数
        if (StringUtils.isNotBlank(ssk) && ssk.equals(SpcyConstant.HCY_SSK_ZZ)) {
            zzrs = fzzrs;
        }else if (StringUtils.isNotBlank(ssk) && ssk.equals(SpcyConstant.HCY_SSK_ZY)){
            zzrs = fzyrs;
        }else if (StringUtils.isNotBlank(ssk) && ssk.contains(SpcyConstant.HCY_SSK_YBK)){
            zzrs = fybrs;
        }

        if (StringUtils.isBlank(ywflCode)){
            ywflCode = basic.getFywflcode();
        }
        ywflCode = this.replaceYwflcode(ywflCode);

        List<RsHcyVo> spcyRsHcyList = new ArrayList<>();
        RsHcyVo rsHcyVo = new RsHcyVo();
        // 存在回避区域
        if (StringUtils.isNotEmpty(hbqy)){
            rsHcyVo.setHbqy(hbqy);
            rsHcyVo.setZytc(zytc);
//            rsHcyVo.setFcsgz(zytc);
            rsHcyVo.setFywflcode(ywflCode); // 业务分类
            rsHcyVo.setFssk(ssk); // 所属库
            rsHcyVo.setFstate(SpcyCommonConstant.JCY_STATE_YX); // 有效
            rsHcyVo.setStatus(SysUserConstant.STATUS_YES); // 账户正常
            rsHcyVo.setFgzzt(SpcyConstant.JCY_ZT_ZG); // 在岗
            rsHcyVo.setFusertype(SpcyConstant.HCY_USERTYPE_JCY);

            /** 药品注册-检查员-在岗+检查日期范围 查询 */
            /*if (DateConversionUtils.isValidDateStringYmd(kssj)){
                rsHcyVo.setFgzzt(null);
                rsHcyVo.setFwcx("是");
                rsHcyVo.setKssj(DateConversionUtils.convertStringToDateYmd(kssj));
            }*/

            // 请假范围查询
            if (DateConversionUtils.isValidDateStringYmd(kssj) && DateConversionUtils.isValidDateStringYmd(jssj)){
                rsHcyVo.setQjfwcx("是");
                rsHcyVo.setKssj(DateConversionUtils.convertStringToDateYmd(kssj));
                rsHcyVo.setJssj(DateConversionUtils.convertStringToDateYmd(jssj));
            }

            spcyRsHcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

            if (CollectionUtils.isEmpty(spcyRsHcyList)) {
                String msg = "";
                if (StringUtils.isBlank(zytc)){
                    msg += "在回避区域【" + hbqy + "】中,";
                }else {
                    msg += "在回避区域【" + hbqy + "】和专业特长【" + zytc + "】中,";
                }

                if (StringUtils.isNotBlank(ssk)){
                    msg += "没有符合[ "+ssk+" ]的人员选派！";
                }else {
                    msg += "没有符合的人员选派!";
                }

                return Result.error(msg);
            }
        }else {
            rsHcyVo.setFssqy(fssqy);
            rsHcyVo.setZytc(zytc);
//            rsHcyVo.setFcsgz(zytc);
            rsHcyVo.setFywflcode(ywflCode); // 业务分类
            rsHcyVo.setFssk(ssk); // 所属库
            rsHcyVo.setFstate(SpcyCommonConstant.JCY_STATE_YX); // 有效
            rsHcyVo.setStatus(SysUserConstant.STATUS_YES); // 账户正常
            rsHcyVo.setFgzzt(SpcyConstant.JCY_ZT_ZG); // 在岗
            rsHcyVo.setFusertype(SpcyConstant.HCY_USERTYPE_JCY);

            /** 药品注册-检查员-在岗+检查日期范围 查询 */
            /*if (DateConversionUtils.isValidDateStringYmd(kssj)){
                rsHcyVo.setFgzzt(null);
                rsHcyVo.setFwcx("是");
                rsHcyVo.setKssj(DateConversionUtils.convertStringToDateYmd(kssj));
            }*/

            // 请假范围查询
            if (DateConversionUtils.isValidDateStringYmd(kssj) && DateConversionUtils.isValidDateStringYmd(jssj)){
                rsHcyVo.setQjfwcx("是");
                rsHcyVo.setKssj(DateConversionUtils.convertStringToDateYmd(kssj));
                rsHcyVo.setJssj(DateConversionUtils.convertStringToDateYmd(jssj));
            }

            spcyRsHcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

            if (CollectionUtils.isEmpty(spcyRsHcyList)){
                String msg = "";
                if (StringUtils.isBlank(zytc)){
                    msg += "在选择区域【" + fssqy + "】中,";
                }else {
                    msg += "在选择区域【" + fssqy + "】和专业特长【" + zytc + "】中,";
                }

                if (StringUtils.isNotBlank(ssk)){
                    msg += "没有符合[ "+ssk+" ]的人员选派！";
                }else {
                    msg += "没有符合的人员选派!";
                }

                return Result.error(msg);
            }
        }

        // 随机选择人员
        List<RsHcyVo> tSpcyInspectors = RandomListUtils.randomList(spcyRsHcyList, zzrs);

        // 不为空人员
        if (CollectionUtils.isNotEmpty(tSpcyInspectors)) {
            Result result = this.initXpHcy(basic, tSpcyInspectors, sqsxId, fusertype, SpcyConstant.XPFS_ZDXZ,kssj, jssj, hcyXpVo.getFdsxpywid());
            if (!result.isSuccess()){
                return Result.error(result.getMessage());
            }
        }else {
            return Result.error("随机人员为空，选派失败");
        }
        return Result.ok("选派成功");
    }

    /**
     * 方案制作环节 - 自动选派检查人员
     * @param sqsxId 申请事项id
     * @param ssk 所属库
     * @param fssqy 选择区域(所属区域)
     * @param fusertype 人员分类（检查员、专家）
     * @param hbqy 回避区域
     * @param ywflCode 业务分类编码
     * @param zytc 专业特长
     * @param jssj 开始时间
     * @param kssj 结束时间
     */
    public Result xpHcyByAutoInHzp(HcyXpVo hcyXpVo,String sqsxId,SpcySqsxBasic basic, String ssk,String fusertype, String zytc,
                                   String hbqy,String fssqy,String ywflCode, String kssj,String jssj,Integer fzzrs,Integer fzyrs,Integer fybrs){
        if (basic == null && StringUtils.isNotBlank(sqsxId)) {
            basic = spcySqsxBaiscService.selectSqsxInfoById(sqsxId);
            if (basic == null) {
                throw new BusinessException("当前申请事项数据不存在，请联系管理员！");
            }
        }
        if (basic.getFcykssj() != null && basic.getFcyjssj() != null){
            kssj = DateConversionUtils.DateToStringYYYYMMDD(basic.getFcykssj());
            jssj = DateConversionUtils.DateToStringYYYYMMDD(basic.getFcyjssj());
        }

        Integer zzrs = 1; // 随机人数
        if (StringUtils.isNotBlank(ssk) && ssk.equals(SpcyConstant.HCY_SSK_ZZ)) {
            zzrs = fzzrs;
        }else if (StringUtils.isNotBlank(ssk) && ssk.equals(SpcyConstant.HCY_SSK_ZY)){
            zzrs = fzyrs;
        }else if (StringUtils.isNotBlank(ssk) && ssk.contains(SpcyConstant.HCY_SSK_YBK)){
            zzrs = fybrs;
        }

        if (StringUtils.isBlank(ywflCode)){
            ywflCode = basic.getFywflcode();
        }
        String jcylx = hcyXpVo.getFlx(); // 检查员类别
        if (StringUtils.isBlank(jcylx) && StringUtils.isNotBlank(ywflCode)){
            jcylx = JcydjbConstant.getJcylxByYwflcode(ywflCode);
        }

        List<RsHcyVo> spcyRsHcyList = new ArrayList<>();
        RsHcyVo rsHcyVo = new RsHcyVo();
        rsHcyVo.setZytc(zytc);
        rsHcyVo.setFlx(jcylx); // 检查员类别
        rsHcyVo.setFssk(ssk); // 所属库
        rsHcyVo.setFstate(SpcyCommonConstant.JCY_STATE_YX); // 有效
        rsHcyVo.setStatus(SysUserConstant.STATUS_YES); // 账户正常
        rsHcyVo.setFgzzt(SpcyConstant.JCY_ZT_ZG); // 在岗
        rsHcyVo.setFusertype(fusertype);
        rsHcyVo.setCxxpjlsqsxid(sqsxId);

        // 存在回避区域
        if (StringUtils.isNotEmpty(hbqy)){
            rsHcyVo.setHbqy(hbqy);
            spcyRsHcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

            if (CollectionUtils.isEmpty(spcyRsHcyList)) {
                String msg = "";
                if (StringUtils.isBlank(zytc)){
                    if (StringUtils.isNotBlank(hbqy)) {
                        msg += "在回避区域【" + hbqy + "】中,";
                    }
                }else {
                    if (StringUtils.isNotBlank(hbqy)) {
                        msg += "在回避区域【" + hbqy + "】和专业特长【" + zytc + "】中,";
                    }else {
                        msg += "在专业特长【" + zytc + "】中,";
                    }
                }

                if (StringUtils.isNotBlank(ssk)){
                    msg += "没有符合[ "+ssk+" ]的人员选派！";
                }else {
                    msg += "没有符合的人员选派!";
                }

                return Result.error(msg);
            }
        }else {
            rsHcyVo.setFssqy(fssqy);

            spcyRsHcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

            if (CollectionUtils.isEmpty(spcyRsHcyList)){
                String msg = "";
                if (StringUtils.isBlank(zytc)){
                    if (StringUtils.isNotBlank(fssqy)) {
                        msg += "在选择区域【" + fssqy + "】中,";
                    }
                }else {
                    if (StringUtils.isNotBlank(fssqy)) {
                        msg += "在选择区域【" + fssqy + "】和专业特长【" + zytc + "】中,";
                    }else {
                        msg += "在专业特长【" + zytc + "】中,";
                    }
                }

                if (StringUtils.isNotBlank(ssk)){
                    msg += "没有符合[ "+ssk+" ]的人员选派！";
                }else {
                    msg += "没有符合的人员选派!";
                }

                return Result.error(msg);
            }
        }

        // 随机选择人员
        List<RsHcyVo> tSpcyInspectors = RandomListUtils.randomList(spcyRsHcyList, zzrs);

        // 不为空人员
        if (CollectionUtils.isNotEmpty(tSpcyInspectors)) {
            Result result = this.initXpHcy(basic, tSpcyInspectors, sqsxId, fusertype, SpcyConstant.XPFS_ZDXZ,kssj, jssj, hcyXpVo.getFdsxpywid());
            if (!result.isSuccess()){
                return Result.error(result.getMessage());
            }
        }else {
            return Result.error("随机人员为空，选派失败");
        }
        return Result.ok("选派成功");
    }

    /**
     * 方案制作环节 - 自动选派检查人员
     * @param sqsxId 申请事项id
     * @param ssk 所属库
     * @param fssqy 选择区域(所属区域)
     * @param fusertype 人员分类（检查员、专家）
     * @param hbqy 回避区域
     * @param ywflCode 业务分类编码
     * @param zytc 专业特长
     * @param jssj 开始时间
     * @param kssj 结束时间
     */
    public Result xpHcyByAutoInYlqx(HcyXpVo hcyXpVo,String sqsxId,SpcySqsxBasic basic,String ssk,String fusertype, String zytc,
                                    String hbqy,String fssqy,String ywflCode, String kssj,String jssj,Integer fzzrs,Integer fzyrs,Integer fybrs){

        if (basic == null && StringUtils.isNotBlank(sqsxId)) {
            basic = spcySqsxBaiscService.selectSqsxInfoById(sqsxId);
            if (basic == null) {
                throw new BusinessException("当前申请事项数据不存在，请联系管理员！");
            }
        }
        if (basic.getFcykssj() != null && basic.getFcyjssj() != null){
            kssj = DateConversionUtils.DateToStringYYYYMMDD(basic.getFcykssj());
            jssj = DateConversionUtils.DateToStringYYYYMMDD(basic.getFcyjssj());
        }

        Integer zzrs = 1; // 随机人数
        if (StringUtils.isNotBlank(ssk) && ssk.equals(SpcyConstant.HCY_SSK_ZZ)) {
            zzrs = fzzrs;
        }else if (StringUtils.isNotBlank(ssk) && ssk.equals(SpcyConstant.HCY_SSK_ZY)){
            zzrs = fzyrs;
        }else if (StringUtils.isNotBlank(ssk) && ssk.contains(SpcyConstant.HCY_SSK_YBK)){
            zzrs = fybrs;
        }

        if (StringUtils.isBlank(ywflCode)){
            ywflCode = basic.getFywflcode();
        }
        String jcylx = hcyXpVo.getFlx(); // 检查员类别
        if (StringUtils.isBlank(jcylx) && StringUtils.isNotBlank(ywflCode)){
            jcylx = JcydjbConstant.getJcylxByYwflcode(ywflCode);
        }

        List<RsHcyVo> spcyRsHcyList = new ArrayList<>();
        RsHcyVo rsHcyVo = new RsHcyVo();
        rsHcyVo.setZytc(zytc);
        rsHcyVo.setFlx(jcylx); // 检查员类别
        rsHcyVo.setFssk(ssk); // 所属库
        rsHcyVo.setFstate(SpcyCommonConstant.JCY_STATE_YX); // 有效
        rsHcyVo.setStatus(SysUserConstant.STATUS_YES); // 账户正常
        rsHcyVo.setFgzzt(SpcyConstant.JCY_ZT_ZG); // 在岗
        rsHcyVo.setFusertype(fusertype);
        rsHcyVo.setCxxpjlsqsxid(sqsxId);

        // 请假范围查询
        /*if (DateConversionUtils.isValidDateStringYmd(kssj) && DateConversionUtils.isValidDateStringYmd(jssj)){
            rsHcyVo.setQjfwcx("是");
            rsHcyVo.setKssj(DateConversionUtils.convertStringToDateYmd(kssj));
            rsHcyVo.setJssj(DateConversionUtils.convertStringToDateYmd(jssj));
        }*/

        // 存在回避区域
        if (StringUtils.isNotEmpty(hbqy)){
            rsHcyVo.setHbqy(hbqy);

            spcyRsHcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

            if (CollectionUtils.isEmpty(spcyRsHcyList)) {
                String msg = "";
                if (StringUtils.isBlank(zytc)){
                    if (StringUtils.isNotBlank(hbqy)) {
                        msg += "在回避区域【" + hbqy + "】中,";
                    }
                }else {
                    if (StringUtils.isNotBlank(hbqy)) {
                        msg += "在回避区域【" + hbqy + "】和专业特长【" + zytc + "】中,";
                    }else {
                        msg += "在专业特长【" + zytc + "】中,";
                    }
                }

                if (StringUtils.isNotBlank(ssk)){
                    msg += "没有符合[ "+ssk+" ]的人员选派！";
                }else {
                    msg += "没有符合的人员选派!";
                }

                return Result.error(msg);
            }
        }else {
            rsHcyVo.setFssqy(fssqy);

            spcyRsHcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

            if (CollectionUtils.isEmpty(spcyRsHcyList)){
                String msg = "";
                if (StringUtils.isBlank(zytc)){
                    if (StringUtils.isNotBlank(fssqy)) {
                        msg += "在选择区域【" + fssqy + "】中,";
                    }
                }else {
                    if (StringUtils.isNotBlank(fssqy)) {
                        msg += "在选择区域【" + fssqy + "】和专业特长【" + zytc + "】中,";
                    }else {
                        msg += "在专业特长【" + zytc + "】中,";
                    }
                }

                if (StringUtils.isNotBlank(ssk)){
                    msg += "没有符合[ "+ssk+" ]的人员选派！";
                }else {
                    msg += "没有符合的人员选派!";
                }

                return Result.error(msg);
            }
        }

        // 随机选择人员
        List<RsHcyVo> tSpcyInspectors = RandomListUtils.randomList(spcyRsHcyList, zzrs);

        // 不为空人员
        if (CollectionUtils.isNotEmpty(tSpcyInspectors)) {
            Result result = this.initXpHcy(basic, tSpcyInspectors, sqsxId, fusertype, SpcyConstant.XPFS_ZDXZ,kssj, jssj, hcyXpVo.getFdsxpywid());
            if (!result.isSuccess()){
                return Result.error(result.getMessage());
            }
        }else {
            return Result.error("随机人员为空，选派失败");
        }
        return Result.ok("选派成功");
    }

    /**
     * 随机选择一个药检所的人
     */
    public Result xpHcyByYjs(SpcySqsxBasic basic,String sqsxId,String fusertype,String kssj,String jssj,List<RsHcyVo> yjsJcyList, String fdsxpywid){
        // 随机选择人员
        List<RsHcyVo> yjsRandomJcyList = RandomListUtils.randomList(yjsJcyList, 1);

        // 不为空人员
        if (CollectionUtils.isNotEmpty(yjsRandomJcyList)) {
            Result result = this.initXpHcy(basic, yjsRandomJcyList, sqsxId, fusertype, SpcyConstant.XPFS_ZDXZ, kssj, jssj, fdsxpywid);
            if (!result.isSuccess()){
                return Result.error(result.getMessage());
            }
        }
        return Result.ok("操作成功");
    }
    /**
     * 手动选派人员
     * @param hcyids 选派人员ids
     * @param sqsxId 申请事项id
     * @param fusertype 核查员类型
     * @param kssj 开始时间
     * @param jssj 结束时间
     */
    public Result xpHcyByHand(RsHcyVo paramBo, String sqsxId, String hcyids, String fusertype, Date kssj, Date jssj){
        if (StringUtils.isBlank(sqsxId)){
            return Result.error("申请事项参数为空！");
        }

        if (StringUtils.isBlank(hcyids)){
            return Result.error("选择参数为空！");
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxInfoById(sqsxId);
        if (sqsx == null){
            return Result.error("当前申请事项数据不存在，请重试!");
        }
        if (sqsx.getFcykssj() == null || sqsx.getFcyjssj() == null){
            return Result.error("请先保存检查时间！");
        }

        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            return Result.error("登录已过期，请重新登录!");
        }

        String kssj2 = DateConversionUtils.DateToStringYYYYMMDD(sqsx.getFcykssj());
        String jssj2 = DateConversionUtils.DateToStringYYYYMMDD(sqsx.getFcyjssj());

        RsHcyVo rsHcyVo = new RsHcyVo();
        rsHcyVo.setIds(hcyids);

        /** 排除在这个检查范围有任务的人员 */
        rsHcyVo.setSfcxxpqj("是");
        rsHcyVo.setCxkssj(kssj2);
        rsHcyVo.setCxjssj(jssj2);

        List<RsHcyVo> rsHcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);
        if (CollectionUtil.isEmpty(rsHcyList)){
            return Result.error("操作失败，该人员在当前检查时间存在选派任务！");
        }
        this.initXpHcy(sqsx, rsHcyList, sqsxId, fusertype, SpcyConstant.XPFS_SDXZ, kssj2, jssj2,"","", paramBo.getFbz(), paramBo.getZdzysf());

        return Result.ok("选派成功");
    }

    /**
     * 预报名-自动选派人员
     * @param sqsxId 申请事项id
     * @param fxptjid 选派条件id
     * @return
     */
    public Result ybmJcyXpByAuto(YbmHcyListVo hcyXpVo, String sqsxId, String ssk, String fusertype, String zytc,
                                 String hbqy, String fssqy, String ywflCode, String kssj, String jssj,
                                 Integer fzzrs, Integer fzyrs, Integer fybrs, String fxptjid){
        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxInfoById(sqsxId);
        if (sqsx == null){
            return Result.error("申请事项不存在，请重试");
        }

        if (StringUtils.isBlank(ywflCode)){
            ywflCode = sqsx.getFywflcode();
        }

        Integer sjrs = 1; // 组长人数
        int sjssk = 0; // 0未知/1：组长/2: 组员/3：预备

        if (StringUtils.isNotBlank(ssk)) {
            if (ssk.equals(SpcyConstant.HCY_SSK_ZZ)) {
                sjrs = fzzrs;
                sjssk = 1;

            } else if (ssk.equals(SpcyConstant.HCY_SSK_ZY)) {
                sjrs = fzyrs;
                sjssk = 2;

            } else if (ssk.contains(SpcyConstant.HCY_SSK_YBK)) {
                sjrs = fybrs;
                sjssk = 3;
            }
        }

        List<YbmHcyListVo> ybmHcyListVos = new ArrayList<>();
        YbmHcyListVo ybmHcyListVo = new YbmHcyListVo();
        if (StringUtils.isNotBlank(hcyXpVo.getIds())){
            ybmHcyListVo.setIds(hcyXpVo.getIds());
        }else {
            ybmHcyListVo.setSqsxid(sqsxId);
        }
        ybmHcyListVo.setFxpzt(SpcyCommonConstant.SQSX_YBMJL_XPZT_WXP);
        ybmHcyListVo.setFssk(ssk); // 所属库
        ybmHcyListVo.setZytc(zytc);
        ybmHcyListVo.setFgzzt(SpcyConstant.JCY_ZT_ZG); // 工作状态 在岗
        ybmHcyListVo.setCxxpjlsqsxid(sqsxId);

        // 存在回避区域
        if (StringUtils.isNotEmpty(hbqy)){
            ybmHcyListVo.setHbqy(hbqy);
            ybmHcyListVos = spcySqsxYbmjlMapper.getYbmHcyList(ybmHcyListVo);

            if (CollectionUtils.isEmpty(ybmHcyListVos)) {
                String msg = "";

                if (StringUtils.isBlank(zytc)){
                    if (StringUtils.isNotBlank(hbqy)) {
                        msg += "在回避区域【" + hbqy + "】中,";
                    }
                }else {
                    if (StringUtils.isNotBlank(hbqy)) {
                        msg += "在回避区域【" + hbqy + "】和专业特长【" + zytc + "】中,";
                    }else {
                        msg += "在专业特长【" + zytc + "】中,";
                    }
                }

                if (StringUtils.isNotBlank(ssk)){
                    msg += "没有符合[ "+ssk+" ]的人员选派！";
                }else {
                    msg += "没有符合的人员选派!";
                }

                return Result.error(msg);
            }
        }else {
            ybmHcyListVo.setFssqy(fssqy);
            ybmHcyListVos = spcySqsxYbmjlMapper.getYbmHcyList(ybmHcyListVo);

            if (CollectionUtils.isEmpty(ybmHcyListVos)) {
                String msg = "";
                if (StringUtils.isBlank(zytc)){
                    if (StringUtils.isNotBlank(fssqy)) {
                        msg += "在选择区域【" + fssqy + "】中,";
                    }
                }else {
                    if (StringUtils.isNotBlank(fssqy)) {
                        msg += "在选择区域【" + fssqy + "】和专业特长【" + zytc + "】中,";
                    }else {
                        msg += "在专业特长【" + zytc + "】中,";
                    }
                }

                if (StringUtils.isNotBlank(ssk)){
                    msg += "没有符合[ "+ssk+" ]的人员选派！";
                }else {
                    msg += "没有符合的人员选派!";
                }
                return Result.error(msg);
            }
        }

        // 随机选择人员
        List<YbmHcyListVo> tSpcyInspectors = RandomListUtils.randomList(ybmHcyListVos, sjrs);

        // 不为空人员
        this.initYbmXpjl(tSpcyInspectors, sqsxId, sqsx, kssj, jssj, fusertype, fxptjid, sjssk);

        return Result.ok("选派人员成功");
    }

    /** 初始化预报名选派记录 */
    private void initYbmXpjl(List<YbmHcyListVo> dataList, String sqsxId, SpcySqsxBasic sqsx, String kssj, String jssj, String fusertype, String fxptjid, int sjssk){
        if (CollectionUtils.isEmpty(dataList)) {
            return;
        }

//        int sjxzrs = 0; // 随机选中人数
        String flowType = SpcyConstant.getFlowType(fusertype); // 查验 、 审评

        String nowUserName = "";
        String nowUserId = "";
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser != null){
            nowUserName = currentUser.getName();
            nowUserId = currentUser.getId();
        }

        for (YbmHcyListVo jcy : dataList) {
            boolean existSqsxHcy = isExistSqsxHcy(jcy.getFuserid(), sqsxId); // 判断当前事项、是否已经存在该人员
            if (existSqsxHcy) {
                continue;
            }

            SpcySqsxXpjl xpjl = new SpcySqsxXpjl();
            xpjl.setId(GetUuIdUtils.ReplaceUuId());
            xpjl.setFuserid(jcy.getFuserid());
            xpjl.setFywflcode(sqsx.getFywflcode());

            xpjl.setFjcyid(jcy.getFjcyid());
            xpjl.setFsqsxid(sqsxId);
            xpjl.setFsftyxp(SpcyConstant.XPJL_SFTYXP_WQY); // 默认未确认

            xpjl.setFrwlx(sqsx.getFjclx());
            xpjl.setFbjcqy(sqsx.getFqymc());
            xpjl.setFjcyxm(jcy.getRealname());

            xpjl.setFsfyjsry(jcy.getFsfyjsry());
            xpjl.setFxllx(jcy.getFxllx());
            xpjl.setFlx(jcy.getFlx());
            xpjl.setFjcyxl(jcy.getFjcyxl());

            xpjl.setFjcydj(jcy.getFjcydj());
            xpjl.setFjzqk(jcy.getFjcyjzqk());

            xpjl.setFsczy(jcy.getFszly());
            xpjl.setFzc(jcy.getFzc());
            xpjl.setFssk(jcy.getFssk());

            xpjl.setFzw(jcy.getFzw());
            xpjl.setFssqy(jcy.getFssqy());
            xpjl.setFgzdw(jcy.getFgzdw());

            xpjl.setFxl(jcy.getFxl());
            xpjl.setFjcylxdh(jcy.getPhone());
            xpjl.setFjcyyx(jcy.getEmail());

            xpjl.setFzprmc(nowUserName);
            xpjl.setFzprid(nowUserId);
            xpjl.setFzpsj(new Date());

            xpjl.setFxpfs(SpcyConstant.XPTJ_XPFS_YBMSJ);// 选派方式

            if (DateConversionUtils.isValidDateStringYmd(kssj)) {
                xpjl.setFgzkssj(DateConversionUtils.convertStringToDateYmd(kssj));
            } else {
                Map sqsxCyOrSpsj = this.getSqsxCyOrSpsj(sqsx, flowType);
                xpjl.setFgzkssj((Date) sqsxCyOrSpsj.get("kssj"));
            }

            if (DateConversionUtils.isValidDateStringYmd(jssj)) {
                xpjl.setFgzjssj(DateConversionUtils.convertStringToDateYmd(jssj));
            } else {
                Map sqsxCyOrSpsj = this.getSqsxCyOrSpsj(sqsx, flowType);
                xpjl.setFgzjssj((Date) sqsxCyOrSpsj.get("jssj"));
            }

            if (StringUtils.isNotBlank(sqsx.getFszdq())) {
                xpjl.setFjcqy(sqsx.getFszdq());
            }

            xpjl.setFspcylx(flowType); // 查验、审评、整改
            xpjl.setFkhlx(SpcyConstant.getXpjlKhlxBySsk(xpjl.getFssk()));

            xpjl.setFdsxpywid("");
            xpjl.setFxptjid(fxptjid);
            xpjl.setFbz("");
            spcySqsxXpjlService.save(xpjl);

//            sjxzrs = sjxzrs +1;

            // 20240423注
//            ycSysUserService.updateUserGzztInZxrw(xpjl.getFuserid());

            if (StringUtils.isNotBlank(jcy.getId())) {
                LambdaUpdateWrapper<SpcySqsxYbmjl> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(SpcySqsxYbmjl::getId, jcy.getId());

                SpcySqsxYbmjl updateBean = new SpcySqsxYbmjl();
                updateBean.setFxpzt(SpcyCommonConstant.SQSX_YBMJL_XPZT_YXP);
                spcySqsxYbmjlMapper.update(updateBean, updateWrapper);
            }
        }

        /** 更新选派条件选中人数 */
        /*if (sjxzrs > 0){
            spcySqsxXptjjlService.updateXptjXzrsById(fxptjid, sjssk, sjxzrs);
        }*/
    }

    /** 初始化预报名和随机检查员库的选派记录数据 */
    private List<SpcySqsxXpjl> initYbmOrJcykXpjlData(List<YbmHcyListVo> dataList, List<RsHcyVo> jcyList,
                                                     String sqsxId, SpcySqsxBasic sqsx, String kssj, String jssj, String fusertype, String fxptjid){

        List<SpcySqsxXpjl> resultXpList = new ArrayList<>();
        String flowType = SpcyConstant.getFlowType(fusertype); // 查验 、 审评

        String nowUserName = "";
        String nowUserId = "";
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser != null){
            nowUserName = currentUser.getName();
            nowUserId = currentUser.getId();
        }

        if (CollectionUtil.isNotEmpty(dataList)) {
            for (YbmHcyListVo jcy : dataList) {
                SpcySqsxXpjl xpjl = new SpcySqsxXpjl();
                xpjl.setFsftyxp(SpcyConstant.XPJL_SFTYXP_WQY); // 默认未确认
                xpjl.setId(GetUuIdUtils.NotReplaceUuId());
                xpjl.setFuserid(jcy.getFuserid());
                xpjl.setFywflcode(sqsx.getFywflcode());

                xpjl.setFjcyid(jcy.getFjcyid());
                xpjl.setFsqsxid(sqsxId);


                xpjl.setFrwlx(sqsx.getFjclx());
                xpjl.setFbjcqy(sqsx.getFqymc());
                xpjl.setFjcyxm(jcy.getRealname());

                xpjl.setFsfyjsry(jcy.getFsfyjsry());
                xpjl.setFxllx(jcy.getFxllx());
                xpjl.setFlx(jcy.getFlx());
                xpjl.setFjcyxl(jcy.getFjcyxl());

                xpjl.setFjcydj(jcy.getFjcydj());
                xpjl.setFjzqk(jcy.getFjcyjzqk());

                xpjl.setFsczy(jcy.getFszly());
                xpjl.setFzc(jcy.getFzc());
                xpjl.setFssk(jcy.getFssk());

                xpjl.setFzw(jcy.getFzw());
                xpjl.setFssqy(jcy.getFssqy());
                xpjl.setFgzdw(jcy.getFgzdw());

                xpjl.setFxl(jcy.getFxl());
                xpjl.setFjcylxdh(jcy.getPhone());
                xpjl.setFjcyyx(jcy.getEmail());

                xpjl.setFzprmc(nowUserName);
                xpjl.setFzprid(nowUserId);
                xpjl.setFzpsj(new Date());

                xpjl.setFxpfs(SpcyConstant.XPTJ_XPFS_YBMSJ);// 选派方式

                if (DateConversionUtils.isValidDateStringYmd(kssj)) {
                    xpjl.setFgzkssj(DateConversionUtils.convertStringToDateYmd(kssj));
                } else {
                    Map sqsxCyOrSpsj = this.getSqsxCyOrSpsj(sqsx, flowType);
                    xpjl.setFgzkssj((Date) sqsxCyOrSpsj.get("kssj"));
                }

                if (DateConversionUtils.isValidDateStringYmd(jssj)) {
                    xpjl.setFgzjssj(DateConversionUtils.convertStringToDateYmd(jssj));
                } else {
                    Map sqsxCyOrSpsj = this.getSqsxCyOrSpsj(sqsx, flowType);
                    xpjl.setFgzjssj((Date) sqsxCyOrSpsj.get("jssj"));
                }

                if (StringUtils.isNotBlank(sqsx.getFszdq())) {
                    xpjl.setFjcqy(sqsx.getFszdq());
                }

                xpjl.setFspcylx(flowType); // 查验、审评、整改
                xpjl.setFkhlx(SpcyConstant.getXpjlKhlxBySsk(xpjl.getFssk()));

                xpjl.setFdsxpywid("");
                xpjl.setFxptjid(fxptjid);
                xpjl.setFbz("");
                xpjl.setCreateTime(new Date());

                resultXpList.add(xpjl);
            }
        }

        if (CollectionUtil.isNotEmpty(jcyList)) {
            for (RsHcyVo hcy : jcyList) {
                SpcySqsxXpjl xpjl = new SpcySqsxXpjl();
                xpjl.setId(GetUuIdUtils.NotReplaceUuId());
                xpjl.setFxpfs(SpcyConstant.XPFS_ZDXZ);// 选派方式
                xpjl.setFsftyxp(SpcyConstant.XPJL_SFTYXP_WQY); // 默认未确认

                xpjl.setId(GetUuIdUtils.ReplaceUuId());
                xpjl.setFuserid(hcy.getFuserid());
                xpjl.setFywflcode(sqsx.getFywflcode());

                xpjl.setFjcyid(hcy.getId());
                xpjl.setFsqsxid(sqsxId);

                xpjl.setFsfyjsry(hcy.getFsfyjsry());
                xpjl.setFrwlx(sqsx.getFjclx());
                xpjl.setFbjcqy(sqsx.getFqymc());
                xpjl.setFjcyxm(hcy.getRealname());

                xpjl.setFxllx(hcy.getFxllx());
                xpjl.setFlx(hcy.getFlx());
                xpjl.setFjcyxl(hcy.getFjcyxl());

                xpjl.setFjcydj(hcy.getFjcydj());
                xpjl.setFjzqk(hcy.getFjcyjzqk());

                xpjl.setFsczy(hcy.getFszly());
                xpjl.setFzc(hcy.getFzc());
                xpjl.setFssk(hcy.getFssk());
                xpjl.setFzw(hcy.getFzw());
                xpjl.setFssqy(hcy.getFssqy());
                xpjl.setFgzdw(hcy.getFgzdw());

                xpjl.setFxl(hcy.getFxl());
                xpjl.setFjcylxdh(hcy.getPhone());
                xpjl.setFjcyyx(hcy.getEmail());

                xpjl.setFzprmc(nowUserName);
                xpjl.setFzprid(nowUserId);
                xpjl.setFzpsj(new Date());

                if (DateConversionUtils.isValidDateStringYmd(kssj)) {
                    xpjl.setFgzkssj(DateConversionUtils.convertStringToDateYmd(kssj));
                } else {
                    Map sqsxCyOrSpsj = this.getSqsxCyOrSpsj(sqsx, flowType);
                    xpjl.setFgzkssj((Date) sqsxCyOrSpsj.get("kssj"));
                }

                if (DateConversionUtils.isValidDateStringYmd(jssj)) {
                    xpjl.setFgzjssj(DateConversionUtils.convertStringToDateYmd(jssj));
                } else {
                    Map sqsxCyOrSpsj = this.getSqsxCyOrSpsj(sqsx, flowType);
                    xpjl.setFgzjssj((Date) sqsxCyOrSpsj.get("jssj"));
                }

                if (StringUtils.isNotBlank(sqsx.getFszdq())) {
                    xpjl.setFjcqy(sqsx.getFszdq());
                }

                xpjl.setFspcylx(flowType); // 查验、审评、整改
                xpjl.setFkhlx(SpcyConstant.getXpjlKhlxBySsk(xpjl.getFssk()));

                xpjl.setFdsxpywid("");
                xpjl.setFxptjid(fxptjid);
                xpjl.setFbz("");
                xpjl.setCreateTime(new Date());

                resultXpList.add(xpjl);
            }
        }

        return resultXpList;
    }

    /** 预报名随机选派+检查员库随机 */
    public Result ybmOrJcykXpByAuto(YbmHcyListVo hcyXpVo, String sqsxId, SpcySqsxBasic sqsx,
                                    String ssk, String fusertype, String zytc, String hbqy,
                                    String fssqy, String ywflCode, String kssj, String jssj,
                                    Integer fzzrs, Integer fzyrs, Integer fybrs, String fxptjid){
        if (sqsx == null) {
            sqsx = spcySqsxBaiscService.selectSqsxInfoById(sqsxId);
        }
        if (sqsx.getFcykssj() == null || sqsx.getFcyjssj() == null){
            return Result.error("请先保存检查时间！");
        }
        Integer sjrs = 1; // 随机人数
//        int sjssk = 0; // 0未知/1：组长/2: 组员/3：预备

        if (StringUtils.isNotBlank(ssk)) {
            if (ssk.equals(SpcyConstant.HCY_SSK_ZZ)) {
                sjrs = fzzrs;

            } else if (ssk.equals(SpcyConstant.HCY_SSK_ZY)) {
                sjrs = fzyrs;

            } else if (ssk.contains(SpcyConstant.HCY_SSK_YBK)) {
                sjrs = fybrs;
            }
        }

        int blMin = sjrs; // 最小比例
        if (blMin<1){
            return Result.ok();
        }

        int blMax = hcyXpVo.getBlMax(); // 最大比例
        int blTotal = blMin*blMax;

        /** 预报名检查员随机 */
        List<YbmHcyListVo> ybmJcyList = new ArrayList<>();
        YbmHcyListVo queryYbmVo = new YbmHcyListVo();
        if (StringUtils.isNotBlank(hcyXpVo.getIds())){
            queryYbmVo.setIds(hcyXpVo.getIds());
        }else {
            queryYbmVo.setSqsxid(sqsxId);
        }
        queryYbmVo.setFxpzt(SpcyCommonConstant.SQSX_YBMJL_XPZT_WXP);
        queryYbmVo.setFssk(ssk); // 所属库
        queryYbmVo.setZytc(zytc);
        queryYbmVo.setFgzzt(SpcyConstant.JCY_ZT_ZG); // 工作状态 在岗
        queryYbmVo.setCxxpjlsqsxid(sqsxId);

        // 存在回避区域
        if (StringUtils.isNotEmpty(hbqy)){
            queryYbmVo.setHbqy(hbqy);
        }else {
            queryYbmVo.setFssqy(fssqy);
        }

        /** 排除在这个检查范围有任务的人员 */
        queryYbmVo.setSfcxxpqj("是");
        queryYbmVo.setCxkssj(DateConversionUtils.DateToStringYYYYMMDD(sqsx.getFcykssj()));
        queryYbmVo.setCxjssj(DateConversionUtils.DateToStringYYYYMMDD(sqsx.getFcyjssj()));

        ybmJcyList = spcySqsxYbmjlMapper.getYbmHcyList(queryYbmVo);

        int ybmsjSize = (ybmJcyList.size()>blTotal) ? sjrs : ybmJcyList.size();
        int syJcykSjSize = (ybmJcyList.size()>blTotal) ? 0 : (blTotal-ybmJcyList.size()); // 剩余检查员库随机数量

        /** 最终选派随机池子 */
        List<SpcySqsxXpjl> sjPoolXpjlList = new ArrayList<>();

        /** 随机选择人员(预报名) */
        List<YbmHcyListVo> ybmSjList = RandomListUtils.randomList(ybmJcyList, ybmsjSize);
        /** 预报名随机不为空人员,将预备名人员全部加入 */
        if (CollectionUtil.isNotEmpty(ybmSjList)) {
            sjPoolXpjlList.addAll(this.initYbmOrJcykXpjlData(ybmSjList, null, sqsxId, sqsx, kssj, jssj, fusertype, fxptjid));
        }

        /** 全库检查员随机，存在剩余的随机人数，
         * 对检查员库进行随机，将人数加入到选派池子
         *  */
        List<RsHcyVo> jcykList = new ArrayList<>();
        if (syJcykSjSize > 0) {
            /*String jcylx = hcyXpVo.getFlx(); // 检查员类别
            if (StringUtils.isBlank(jcylx) && StringUtils.isNotBlank(ywflCode)){
                jcylx = JcydjbConstant.getJcylxByYwflcode(ywflCode);
            }*/
            String jcylx = JcydjbConstant.getJcylxByYwflcode(ywflCode);

            RsHcyVo queryJcyVo = new RsHcyVo();
            queryJcyVo.setZytc(zytc);
            queryJcyVo.setFlx(jcylx);
            queryJcyVo.setFssk(ssk); // 所属库
            queryJcyVo.setFstate(SpcyCommonConstant.JCY_STATE_YX); // 有效
            queryJcyVo.setFgzzt(SpcyConstant.JCY_ZT_ZG); // 在岗
            queryJcyVo.setFusertype(SpcyConstant.HCY_USERTYPE_JCY);
            queryJcyVo.setCxxpjlsqsxid(sqsxId);

            // 20240709 高频库
            if (StringUtils.isNotBlank(hcyXpVo.getFgpk()) && hcyXpVo.getFgpk().equals(SpcyConstant.JCY_GKP_YES)) {
                queryJcyVo.setFgpk(SpcyConstant.JCY_GKP_YES);
            }else {
                queryJcyVo.setFgpk(null);
            }

            // 存在回避区域
            /*if (StringUtils.isNotEmpty(hbqy)){
                queryJcyVo.setHbqy(hbqy);
            }else {
                queryJcyVo.setFssqy(fssqy);
            }*/

            /** 排除在这个检查范围有任务的人员 */
            queryJcyVo.setSfcxxpqj("是");
            queryJcyVo.setCxkssj(DateConversionUtils.DateToStringYYYYMMDD(sqsx.getFcykssj()));
            queryJcyVo.setCxjssj(DateConversionUtils.DateToStringYYYYMMDD(sqsx.getFcyjssj()));

            if (StringUtils.isBlank(hcyXpVo.getSfhbydw()) || "回避".equals(hcyXpVo.getSfhbydw())){
                queryJcyVo.setHbydw(sqsx.getFqymc());
            }
            jcykList = spcyRsHcyService.getRsHcyList(queryJcyVo);

            /** 现有池子的用户id */
            if (CollectionUtil.isNotEmpty(sjPoolXpjlList)) {
                List<String> poolUserIdList = sjPoolXpjlList.stream()
                        .map(SpcySqsxXpjl::getFuserid)
                        .collect(Collectors.toList());

                /** 筛选还没在随机池子的用户 */
                jcykList = jcykList.stream()
                        .filter(f -> !poolUserIdList.contains(f.getFuserid()))
                        .collect(Collectors.toList());
            }

            /** 随机选择人员(检查员库) */
            List<RsHcyVo> jcykSjList = RandomListUtils.randomList(jcykList, syJcykSjSize);
            if (CollectionUtil.isNotEmpty(jcykSjList)){
                sjPoolXpjlList.addAll(this.initYbmOrJcykXpjlData(null, jcykSjList, sqsxId, sqsx, kssj, jssj, fusertype, fxptjid));
            }
        }

        if (CollectionUtil.isEmpty(sjPoolXpjlList)){
            return Result.error(ssk + "没有满足的人员！");
        }

        /** 在池子进行最终随机选人 */
        List<SpcySqsxXpjl> poolXpjlBaseList = RandomListUtils.randomList(sjPoolXpjlList, blMin);
        spcySqsxXpjlService.batchAddXpjlList(poolXpjlBaseList);

        return Result.ok();
    }

    /**
     * 检查员库随机-药检所人员-指派
     */
    public Result yjsrySjXpByFxptjid(YbmHcyListVo hcyXpVo, String sqsxid, SpcySqsxBasic sqsx,
                                     String fusertype, String zytc, String hbqy,
                                     String fssqy, String ywflCode, String kssj, String jssj,
                                     Integer fzzrs, Integer fzyrs, Integer fybrs, String fxptjid){

        if (fzzrs == 0 && fzyrs == 0){
            return Result.ok();
        }

        if (StringUtils.isBlank(hcyXpVo.getZdyjsry()) || !hcyXpVo.getZdyjsry().equals("指派")){
            return Result.ok();
        }

        List<SpcySqsxXpjl> tjxpList = spcySqsxXpjlService.list(new LambdaQueryWrapper<SpcySqsxXpjl>()
                .eq(SpcySqsxXpjl::getFsqsxid, sqsxid)
                .eq(SpcySqsxXpjl::getFxptjid, fxptjid));

        List<SpcySqsxXpjl> poolYjsJcyList = tjxpList.stream()
                .filter(f -> StringUtils.isNotBlank(f.getFsfyjsry()) && f.getFsfyjsry().equals(SpcyConstant.HCY_SFYJSRY_YES))
                .collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(poolYjsJcyList)){
            return Result.ok();
        }

        String ssk = "";
        if (fzzrs > 0 && fzyrs > 0){

        }else if (fzzrs > 0 && fzyrs == 0){
            ssk= SpcyConstant.HCY_SSK_ZZ;
        }else if (fzzrs == 0 && fzyrs > 0){
            ssk = SpcyConstant.HCY_SSK_ZY;
        }

        String jcylx = JcydjbConstant.getJcylxByYwflcode(ywflCode);
        RsHcyVo yjsjcy = new RsHcyVo();
        yjsjcy.setZytc(zytc);
        yjsjcy.setFlx(jcylx);
        yjsjcy.setFssk(ssk); // 所属库
        yjsjcy.setFstate(SpcyCommonConstant.JCY_STATE_YX); // 有效
        yjsjcy.setFgzzt(SpcyConstant.JCY_ZT_ZG); // 在岗
        yjsjcy.setFusertype(fusertype);
        yjsjcy.setFsfyjsry(SpcyConstant.HCY_SFYJSRY_YES); // 为药检所人员
        yjsjcy.setCxxpjlsqsxid(sqsxid);

        // 2024-07-09
        if (StringUtils.isNotBlank(hcyXpVo.getFgpk()) && hcyXpVo.getFgpk().equals(SpcyConstant.JCY_GKP_YES)){
            yjsjcy.setFgpk(SpcyConstant.JCY_GKP_YES);
        }else {
            yjsjcy.setFgpk(null);
        }

        /** 排除在这个检查范围有任务的人员 */
        yjsjcy.setSfcxxpqj("是");
        yjsjcy.setCxkssj(DateConversionUtils.DateToStringYYYYMMDD(sqsx.getFcykssj()));
        yjsjcy.setCxjssj(DateConversionUtils.DateToStringYYYYMMDD(sqsx.getFcyjssj()));

        // 存在回避区域
        if (StringUtils.isNotEmpty(hbqy)) {
            yjsjcy.setHbqy(hbqy);
        } else {
            yjsjcy.setFssqy(fssqy);
        }
        List<RsHcyVo> yjsJcyList = spcyRsHcyService.getRsHcyList(yjsjcy);
        if (CollectionUtil.isEmpty(yjsJcyList)){
            return Result.ok();
        }

        /** 药检所全库随机一个人员 */
        List<RsHcyVo> yjsRandomJcyList = RandomListUtils.randomList(yjsJcyList, 1);

        /** 初始化选派 */
        Result result = this.initXpHcy(sqsx, yjsRandomJcyList, sqsxid, fusertype, SpcyConstant.XPFS_ZDXZ, kssj, jssj, "", fxptjid);
        if (!result.isSuccess()){
            return Result.error(result.getMessage());
        }

        /** 剔除对应身份的检查员选派 */
        List<SpcySqsxXpjl> removetjxpJcyList = tjxpList.stream()
                .filter(f -> StringUtils.isNotBlank(f.getFssk()) && f.getFssk().equals(yjsRandomJcyList.get(0).getFssk()))
                .collect(Collectors.toList());

        /** 剔除已选身份人员 */
        List<SpcySqsxXpjl> tjxpRandomJcyList = RandomListUtils.randomList(removetjxpJcyList, 1);
        if (CollectionUtil.isNotEmpty(tjxpRandomJcyList)){
            spcySqsxXpjlService.removeById(tjxpRandomJcyList.get(0).getId());
        }


        return Result.ok();
    }

    /**
     * 删除选派人员
     * @param sqsxId 申请事项id
     * @param xpjlId 选派记录id
     */
    public Result deleteXpHcy(String sqsxId,String xpjlId){
        if (StringUtils.isEmpty(xpjlId)){
            return Result.error("选派记录id不能为空!");
        }
        if (StringUtils.isEmpty(sqsxId)){
            return Result.error("申请事项id不能为空!");
        }

        String nowNodeName = ""; // 当前环节名称
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            return Result.error("登录已过期，请重新登录!");
        }

        nowNodeName = getNowNodeName(sqsxId); // 获取当前环节名称

        // 当前选派记录
        SpcySqsxXpjl xpjl = spcySqsxXpjlService.getById(xpjlId);
        if (xpjl == null){
            return Result.error("当前选派记录不存在，请重试!");
        }
        String jcyId = xpjl.getFjcyid(); // 核查员id
        spcySqsxXpjlService.removeById(xpjlId); // 删除选派记录

        String xpContent = "删除选派记录：";
        Map<String, Object> xpJlMap = ObjectToMapUtils.objectToMap(xpjl);
        xpContent += LogUtils.MapToStringMethod(xpJlMap);
        spcyLogService.addLogs(nowNodeName,xpContent,sqsxId,LogConstant.LOGS_SQSX_XPJL,SpcyConstant.LOGS_SYS_TYPE_SC);

        SpcyRsHcy hcy = spcyRsHcyService.getById(jcyId);
        YcSysUser user = null;
        if (hcy != null) {
            user = ycSysUserService.getById(hcy.getFuserid());
        }
        // 是否为执行任务中
        if (user != null && user.getFgzzt().equals(SpcyConstant.JCY_ZY_ZXRWZ)){
            user.setFgzzt(SpcyConstant.JCY_ZT_ZG); // 将核查员该为 在岗
            ycSysUserService.updateById(user);

            String operCont = "基本信息修改：将【核查员工作状态】从[执行任务中]设置为[在岗]";
            spcyLogService.addLogs(nowNodeName, operCont, sqsxId, LogConstant.LOGS_RS_HCY, SpcyConstant.LOGS_SYS_TYPE_XG);
        }

        return Result.ok("删除成功");
    }

    /**
     * 初始化选派核查员
     */
    public Result initXpHcy(SpcySqsxBasic sqsx,List<RsHcyVo> hcyList,String sqsxId,String fusertype,String xpfs,String kssj,String jssj, String fdsxpywid){
        return this.initXpHcy(sqsx, hcyList, sqsxId, fusertype, xpfs, kssj, jssj, fdsxpywid,"","");
    }

    public Result initXpHcy(SpcySqsxBasic sqsx,List<RsHcyVo> hcyList,String sqsxId,String fusertype,String xpfs,String kssj,String jssj, String fdsxpywid, String fxptjid){
        return this.initXpHcy(sqsx, hcyList, sqsxId, fusertype, xpfs, kssj, jssj, fdsxpywid,fxptjid, "");
    }

    public Result initXpHcy(SpcySqsxBasic sqsx,List<RsHcyVo> hcyList,String sqsxId,String fusertype,String xpfs,String kssj,String jssj, String fdsxpywid, String fxptjid, String fbz){
        return this.initXpHcy(sqsx, hcyList, sqsxId, fusertype, xpfs, kssj, jssj, fdsxpywid, fxptjid, fbz, "");
    }

    public Result initXpHcy(SpcySqsxBasic sqsx,List<RsHcyVo> hcyList,String sqsxId,String fusertype,String xpfs,String kssj,String jssj, String fdsxpywid, String fxptjid, String fbz, String zyzysf){
        if (StringUtils.isBlank(fxptjid)){
            fxptjid = GetUuIdUtils.ReplaceUuId();
        }

        String flowType = SpcyConstant.getFlowType(fusertype); // 查验 、 审评、整改
        String nowUserName = "";
        String nowUserId = "";
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser != null){
            nowUserName = currentUser.getName();
            nowUserId = currentUser.getId();
        }

        int xzzzrs = 0; // 选中组长人数
        int xzzyrs = 0; // 选中组员人数
        int xzybrs = 0; // 选中预备人数

        String msg = "";
        for (RsHcyVo hcy : hcyList) {

            /** 判断当前事项、是否已经存在该人员 */
            boolean existSqsxHcy = isExistSqsxHcy(hcy.getFuserid(), sqsxId);
            if (existSqsxHcy){
                msg += hcy.getRealname() + "("+hcy.getFssk()+")"+"当前事项已选派过该人员，请不要重复选择！";
                continue;
            }

            SpcySqsxXpjl xpjl = new SpcySqsxXpjl();
            xpjl.setFxpfs(xpfs);// 选派方式

            xpjl.setFssk(hcy.getFssk());
            if (xpfs.equals(SpcyConstant.XPFS_SDXZ)) {
                xpjl.setFsftyxp(SpcyConstant.XPJL_SFTYXP_YES);

                /** 指定将组长改为组员 */
                if (xpjl.getFssk().equals(SpcyConstant.HCY_SSK_ZZ)
                        && StringUtils.isNotBlank(zyzysf) && zyzysf.equals("组员")){
                    xpjl.setFssk(SpcyConstant.HCY_SSK_ZY);
                }

                /** 验证选派人数是否已经满足 */
                boolean flag = this.verifySfSendSmsByFssk(null, sqsxId, xpjl.getFssk());
                if (!flag){
                    msg += "选派"+hcy.getRealname()+"("+xpjl.getFssk()+")失败，当前事项预设的人数已满足！";
                    continue;
                }
            }else {
                xpjl.setFsftyxp(SpcyConstant.XPJL_SFTYXP_WQY); // 默认未确认
            }

            xpjl.setId(GetUuIdUtils.ReplaceUuId());
            xpjl.setFuserid(hcy.getFuserid());
            xpjl.setFywflcode(sqsx.getFywflcode());

            xpjl.setFjcyid(hcy.getId());
            xpjl.setFsqsxid(sqsxId);


            xpjl.setFrwlx(sqsx.getFjclx());
            xpjl.setFbjcqy(sqsx.getFqymc());
            xpjl.setFjcyxm(hcy.getRealname());

            xpjl.setFxllx(hcy.getFxllx());
            xpjl.setFlx(hcy.getFlx());
            xpjl.setFjcyxl(hcy.getFjcyxl());

            xpjl.setFjcydj(hcy.getFjcydj());
            xpjl.setFjzqk(hcy.getFjcyjzqk());

            xpjl.setFsczy(hcy.getFszly());
            xpjl.setFzc(hcy.getFzc());

            if (StringUtils.isNotBlank(xpjl.getFssk())) {
                if (xpjl.getFssk().equals(SpcyConstant.HCY_SSK_ZZ)) {
                    xzzzrs = xzzzrs + 1;

                } else if (xpjl.getFssk().equals(SpcyConstant.HCY_SSK_ZY)) {
                    xzzyrs = xzzyrs + 1;

                } else if (xpjl.getFssk().equals(SpcyConstant.HCY_SSK_YB)) {
                    xzybrs = xzybrs + 1;
                }
            }

            xpjl.setFsfyjsry(hcy.getFsfyjsry());
            xpjl.setFzw(hcy.getFzw());
            xpjl.setFssqy(hcy.getFssqy());
            xpjl.setFgzdw(hcy.getFgzdw());

            xpjl.setFxl(hcy.getFxl());
            xpjl.setFjcylxdh(hcy.getPhone());
            xpjl.setFjcyyx(hcy.getEmail());

            xpjl.setFzprmc(nowUserName);
            xpjl.setFzprid(nowUserId);
            xpjl.setFzpsj(new Date());

            if (DateConversionUtils.isValidDateStringYmd(kssj)) {
                xpjl.setFgzkssj(DateConversionUtils.convertStringToDateYmd(kssj));
            }else {
                Map sqsxCyOrSpsj = this.getSqsxCyOrSpsj(sqsx, flowType);
                xpjl.setFgzkssj((Date) sqsxCyOrSpsj.get("kssj"));
            }

            if (DateConversionUtils.isValidDateStringYmd(jssj)) {
                xpjl.setFgzjssj(DateConversionUtils.convertStringToDateYmd(jssj));
            }else {
                Map sqsxCyOrSpsj = this.getSqsxCyOrSpsj(sqsx, flowType);
                xpjl.setFgzjssj((Date) sqsxCyOrSpsj.get("jssj"));
            }

            if (StringUtils.isNotBlank(sqsx.getFszdq())) {
                xpjl.setFjcqy(sqsx.getFszdq());
            }

            xpjl.setFspcylx(flowType); // 查验、审评、整改
            xpjl.setFkhlx(SpcyConstant.getXpjlKhlxBySsk(xpjl.getFssk()));

            xpjl.setFdsxpywid(fdsxpywid);
            xpjl.setFxptjid(fxptjid);
            xpjl.setFbz(fbz);

            spcySqsxXpjlService.save(xpjl);

            /** 自动循环抽选对应身份抽选人数+1 */
            if (StringUtils.isNotBlank(fdsxpywid)){
                if (xpjl.getFssk().equals(SpcyConstant.HCY_SSK_ZZ)) {
                    spcySqsxYwxxMapper.updateCxzzrsAddOneById(fdsxpywid);

                } else if (xpjl.getFssk().equals(SpcyConstant.HCY_SSK_ZY)) {
                    spcySqsxYwxxMapper.updateCxzyrsAddOneById(fdsxpywid);

                } else if (xpjl.getFssk().equals(SpcyConstant.HCY_SSK_YB)) {
                    spcySqsxYwxxMapper.updateCxybrsAddOneById(fdsxpywid);
                }
            }

            /*String xpCon = xpfs+"核查员：";
            Map<String, Object> xpMap = ObjectToMapUtils.objectToMap(xpjl);
            xpCon += LogUtils.MapToStringMethod(xpMap);
            spcyLogService.addLogs(nowNodeName, xpCon, sqsxId, LogConstant.LOGS_SQSX_XPJL, SpcyConstant.LOGS_SYS_TYPE_XZ);*/

            // 是否为选派整改人员
            /*if (StringUtils.isNotBlank(flowType) && flowType.equals(SpcyConstant.FLOW_TYPE_ZG)){
                // 不处理
            }else {

                *//*if (xpfs.equals(SpcyConstant.XPFS_SDXZ)){
                    ycSysUserService.updateUserGzztInZxrw(xpjl.getFuserid());
                }*//*
            }*/

        }

        /** 初始化筛选条件记录 */
        if (xpfs.equals(SpcyConstant.XPFS_SDXZ)) {
            spcySqsxXptjjlService.initXptjjl(fxptjid, sqsxId, xpfs, new HcyXpVo(), xzzzrs, xzzyrs, xzybrs, fbz);

            if (StringUtils.isNotBlank(msg)){
                throw new BusinessException(msg);
            }
        }

        return Result.ok("初始化成功");
    }

    /** 初始化数据修订选派人员 */
    public Result initSjxdXpHcy(SpcySqsxBasic sqsx,List<RsHcyVo> hcyList,
                                String sqsxId,String fusertype,
                                String kssj,String jssj,
                                String xdryid, String xdrymc,
                                String fbz, String zyzysf){
        String flowType = SpcyConstant.getFlowType(fusertype); // 查验 、 审评、整改
        String nowUserName = xdrymc;
        String nowUserId = xdryid;

        for (RsHcyVo hcy : hcyList) {

            spcySqsxXpjlService.remove(new LambdaQueryWrapper<SpcySqsxXpjl>()
                    .eq(SpcySqsxXpjl::getFsqsxid, sqsxId)
                    .eq(SpcySqsxXpjl::getFuserid, hcy.getFuserid()));

            SpcySqsxXpjl xpjl = new SpcySqsxXpjl();
            xpjl.setFxpfs(SpcyConstant.XPFS_SDXZ);// 选派方式

            xpjl.setFsftyxp(SpcyConstant.XPJL_SFTYXP_YES);
            xpjl.setFssk(zyzysf);

            xpjl.setId(GetUuIdUtils.ReplaceUuId());
            xpjl.setFuserid(hcy.getFuserid());
            xpjl.setFywflcode(sqsx.getFywflcode());

            xpjl.setFjcyid(hcy.getId());
            xpjl.setFsqsxid(sqsxId);


            xpjl.setFrwlx(sqsx.getFjclx());
            xpjl.setFbjcqy(sqsx.getFqymc());
            xpjl.setFjcyxm(hcy.getRealname());

            xpjl.setFxllx(hcy.getFxllx());
            xpjl.setFlx(hcy.getFlx());
            xpjl.setFjcyxl(hcy.getFjcyxl());

            xpjl.setFjcydj(hcy.getFjcydj());
            xpjl.setFjzqk(hcy.getFjcyjzqk());

            xpjl.setFsczy(hcy.getFszly());
            xpjl.setFzc(hcy.getFzc());

            xpjl.setFsfyjsry(hcy.getFsfyjsry());
            xpjl.setFzw(hcy.getFzw());
            xpjl.setFssqy(hcy.getFssqy());
            xpjl.setFgzdw(hcy.getFgzdw());

            xpjl.setFxl(hcy.getFxl());
            xpjl.setFjcylxdh(hcy.getPhone());
            xpjl.setFjcyyx(hcy.getEmail());

            xpjl.setFzprmc(nowUserName);
            xpjl.setFzprid(nowUserId);
            xpjl.setFzpsj(new Date());

            if (DateConversionUtils.isValidDateStringYmd(kssj)) {
                xpjl.setFgzkssj(DateConversionUtils.convertStringToDateYmd(kssj));
            }else {
                Map sqsxCyOrSpsj = this.getSqsxCyOrSpsj(sqsx, flowType);
                xpjl.setFgzkssj((Date) sqsxCyOrSpsj.get("kssj"));
            }

            if (DateConversionUtils.isValidDateStringYmd(jssj)) {
                xpjl.setFgzjssj(DateConversionUtils.convertStringToDateYmd(jssj));
            }else {
                Map sqsxCyOrSpsj = this.getSqsxCyOrSpsj(sqsx, flowType);
                xpjl.setFgzjssj((Date) sqsxCyOrSpsj.get("jssj"));
            }

            if (StringUtils.isNotBlank(sqsx.getFszdq())) {
                xpjl.setFjcqy(sqsx.getFszdq());
            }

            xpjl.setFspcylx(flowType); // 查验、审评、整改
            xpjl.setFkhlx(SpcyConstant.getXpjlKhlxBySsk(xpjl.getFssk()));
            xpjl.setFbz(fbz);

            spcySqsxXpjlService.save(xpjl);

            String operContent = "数据修订-新增选派人员：";
            operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(xpjl));
            spcyLogService.addLogs(LogConstant.LOGS_SQSX_XPJL, operContent, sqsxId, "数据修订-新增选派人员", SpcyConstant.LOGS_SYS_TYPE_XZ);

        }

        return Result.ok("初始化成功");
    }

    /**
     * 判断该申请事项、人员选派是否存在
     * @param userid
     * @param sqsxid
     * @return
     */
    public boolean isExistSqsxHcy(String userid,String sqsxid){
        boolean isExist = true;
        List<String> list = spcySqsxXpjlService.selectIdByFsqsxidAndFuserid(sqsxid, userid);
        if (CollectionUtils.isEmpty(list)){
            isExist = false;
        }

        return isExist;
    }
    /** -------------------------------------------【 发送短信 - 功能 】--------------------------------------------------------- */

    /**
     * 每个环节发送短信给待办人
     * @param job 当前流程任务job
     */
    public void sendSmsToPassJob(SpcySqsxJob job){
        String fdbrid = job.getFdbrid(); // 待办人ids
        String nodeName = job.getFhjmc(); // 环节名称

        if (job == null){
            log.info("发送短信失败，流程任务不存在");
            return;
        }
        if (StringUtils.isBlank(fdbrid)){
            log.info("发送短信失败，待办人为空");
            return;
        }

        String msg = "";
        msg = this.getNextNodeSmsContent(job, null);
        this.sendSmsToNodeDbr(fdbrid,job.getFlcmc()+"-"+nodeName,msg);

    }

    /**
     * 给待办人发送短信内容
     * @param job 流程任务job
     * @param smsContent 短信内容
     */
    public void sendNextNodeSmsContent(SpcySqsxJob job,String smsContent){
        if (job == null){
            log.info("发送短信失败，流程任务不存在");
            return;
        }

        String fdbrid = job.getFdbrid(); // 待办人ids

        if (StringUtils.isBlank(fdbrid)){
            log.info("发送短信失败，待办人为空");
            return;
        }

        String msg = "";
        List<YcSysUser> userList = ycSysUserService.findUserJbxxListByIds(StringPatternUtils.getListByIds(fdbrid));
        for (YcSysUser user : userList){
            if (StringUtils.isBlank(user.getPhone())){
                continue;
            }

            msg = "";
            msg += smsContent + "\n";
            try {
                smsUtils.sendSms(user.getPhone(), msg);
            }catch (Exception e){
                log.error("发送短信失败-{}",e.getMessage());
            }
        }
    }

    /**
     * 上环节 jobid - 发送短信
     * @param jobid
     * @param smsContent 短信内容
     */
    public void sendSmsToNextJob(String jobid, String smsContent){
        List<SpcySqsxJob> preJobList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                .eq(SpcySqsxJob::getFsyhjid, jobid)
                .eq(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_DB)
                .orderByDesc(SpcySqsxJob::getFfssj));
        if (CollectionUtils.isNotEmpty(preJobList)){
            if (SpcyFlowConstant.NOT_SEND_FLOW_SMS_HJMC_LIST.contains(preJobList.get(0).getFhjmc())){
                return;
            }

            if (StringUtils.isBlank(smsContent)) {
                this.sendSmsToPassJob(preJobList.get(0)); // 发送短信
            }else {
                this.sendNextNodeSmsContent(preJobList.get(0),smsContent);
            }
        }
    }

    /**
     * 每个环节发送短信给待办人
     * @param dbrId 待办人id
     * @param hjmc 环节名称
     */
    public void sendSmsToNodeDbr(String dbrId,String hjmc,String sms){
        String msg = "";
        if (StringUtils.isEmpty(dbrId)){
            return;
        }

        if (StringUtils.isBlank(sms)){
            sms = "您有一条处于[ "+hjmc+" ]环节的待办信息,请及时前往[ "+SpcyConstant.System_Name+" -> "+SpcyConstant.System_Url+" ]查看，进行办理。";
        }

        List<YcSysUser> userList = ycSysUserService.findUserJbxxListByIds(StringPatternUtils.getListByIds(dbrId));
        for (YcSysUser user : userList){
            if (StringUtils.isBlank(user.getPhone())){
                continue;
            }

            msg = "";
//            msg = "尊敬的"+user.getRealname()+"：\n";
            msg += sms + "\n";

            try {
                smsUtils.sendSms(user.getPhone(),msg);
            }catch (Exception e){
                log.error("发送短信失败-{}",e.getMessage());
            }

        }
    }

    /**
     * 每个环节发送短信给待办人
     * @param dbrId 待办人id
     * @param hjmc 环节名称
     */
    public void sendSmsToNodeDbr(String dbrId,String hjmc){
        this.sendSmsToNodeDbr(dbrId,hjmc,null);
    }

    /** 获取发送下环节待办短信内容 */
    public String getNextNodeSmsContent(SpcySqsxBasic sqsx,String lcmc,String nodeName){
        String smsContent = "";

        if (StringUtils.isNotBlank(nodeName)) {
            smsContent += "您有一条";
        }
        if (sqsx != null) {

            if (org.apache.commons.lang3.StringUtils.isNotBlank(sqsx.getFsqdbh())) {
                smsContent += "申请单号[" + sqsx.getFsqdbh() + "],";
            }

            if (org.apache.commons.lang3.StringUtils.isNotBlank(sqsx.getFsxmc())) {
                smsContent += "申请事项[" + sqsx.getFsxmc() + "]的";
            }
        }

        if (StringUtils.isNotBlank(lcmc)) {
            smsContent += lcmc;
            smsContent += "-";
        }

        if (StringUtils.isNotBlank(nodeName)) {
            smsContent += "处于[ " + nodeName + " ]环节的待办信息,请及时前往[ " + SpcyConstant.System_Name + " -> " + SpcyConstant.System_Url + " ]查看，进行办理。\n";
        }

        return smsContent;
    }

    /**
     * 获取发送下环节待办短信内容
     * @param jobid
     * @return
     */
    public String getNextNodeSmsContent(SpcySqsxJob nowJob,String jobid){

        StringBuffer defaultMsg = new StringBuffer();

        // 当前流程任务id
        if (nowJob == null) {
            if (StringUtils.isBlank(jobid)){
                return "";
            }

            nowJob = spcySqsxJobService.getById(jobid);

            if (nowJob == null) {
                return "";
            }
        }

        String hjmc = nowJob.getFhjmc(); // 环节名称

        defaultMsg.append("您有一条[ "+nowJob.getFlcmc()+" ]处于[ ").append(hjmc).append(" ]环节的[ 待办 ]信息,请及时前往[ ");
        defaultMsg.append(SpcyConstant.System_Name).append(" -> ").append(SpcyConstant.System_Url);
        defaultMsg.append(" ]查看，进行办理。\n");

        String sxmc = ""; // 事项名称
        String blzt = nowJob.getFblzt(); // 办理状态

        String sqsxid = nowJob.getFpid(); // 申请事项id

        if (StringUtils.isBlank(sqsxid)){
            return defaultMsg.toString();
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return defaultMsg.toString();
        }
        sxmc = sqsx.getFsxmc();

        StringBuffer msg = new StringBuffer();
/*        String username = "";
        msg.append("尊敬的").append(username).append("：\n");*/
        msg.append("您有一条事项名称[ ").append(sxmc).append(" ]处于[ ").append(nowJob.getFlcmc()).append("-");
        msg.append(hjmc).append(" ]的[ ").append(blzt).append(" ]信息，\n");
        msg.append("请及时前往[ ").append(SpcyConstant.System_Name).append("->").append(SpcyConstant.System_Url).append(" ]查看，进行办理。");

        return msg.toString();
    }

    /**
     * 发送短信和邮件给 核查员、观察员、企业 (查验)
     * @param sqsxid
     * @return
     */
    public Result sendSmsAndEmailToHcyGcyQy(String sqsxid){
        List<SpcySqsxWjjl> wjjlList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                .eq(StringUtils.isNotBlank(sqsxid), SpcySqsxWjjl::getFpid, sqsxid));

        if (CollectionUtils.isNotEmpty(wjjlList)) {
            // 发生检查员 选派函
            List<SpcySqsxWjjl> jcyxphList = wjjlList.stream().filter(x -> StringUtils.isNotBlank(x.getFwjlx1())
                    && x.getFwjlx1().equals(WjmlConstant.xpxcscydh)).collect(Collectors.toList());

            if (CollectionUtils.isNotEmpty(jcyxphList)){
                for (SpcySqsxWjjl wjjl : jcyxphList) {
                    // 选派现场审查员的函
                    Result result = this.sendSmsToHcy(sqsxid, wjjl.getFbusid(), wjjl.getId(), wjjl);// 短信
                    if (!result.isSuccess()) {
                        return result;
                    }

                    Result result1 = this.sendEmailToHcy(sqsxid, wjjl.getFbusid(), wjjl.getId(), wjjl, SpcyConstant.HCY_USERTYPE_JCY);// 邮箱
                    if (!result1.isSuccess()) {
                        return result1;
                    }
                }
            }

            // 发生观察员 函
            List<SpcySqsxWjjl> gcyhList = wjjlList.stream().filter(x -> StringUtils.isNotBlank(x.getFwjlx1())
                    && x.getFwjlx1().equals(WjmlConstant.xpxcgcydh)).collect(Collectors.toList());

            if (CollectionUtils.isNotEmpty(gcyhList)){
                for (SpcySqsxWjjl wjjl : gcyhList) {
                    // 选派现场观察员的函
                    Result result = this.sendSmsToGcy(sqsxid, wjjl.getId(), wjjl);// 短信
                    if (!result.isSuccess()){
                        return result;
                    }

                    Result result1 = this.sendEmailToGcy(sqsxid, wjjl.getId(), wjjl);// 邮箱
                    if (!result1.isSuccess()){
                        return result1;
                    }
                }
            }

            // 发生企业 函
            List<SpcySqsxWjjl> qyhList = wjjlList.stream().filter(x -> StringUtils.isNotBlank(x.getFwjlx1())
                    && x.getFwjlx1().equals(WjmlConstant.qydwjctz)).collect(Collectors.toList());

            if (CollectionUtils.isNotEmpty(qyhList)){
                for (SpcySqsxWjjl wjjl : qyhList) {
                    // 企业单位检查通知
                    Result result = this.sendSmsToQy(sqsxid, wjjl.getId(), wjjl);// 短信
                    if (!result.isSuccess()){
                        return result;
                    }

                    Result result1 = this.sendEmailToQy(sqsxid, wjjl.getId(), wjjl);// 邮箱
                    if (!result1.isSuccess()){
                        return result1;
                    }
                }
            }
        }

        return Result.ok();
    }

    /**
     * 发送短信和邮件给 专家、企业 (审评)
     * @param sqsxid
     * @return
     */
    public Result sendSmsAndEmailToZjQy(String sqsxid){
        List<SpcySqsxWjjl> wjjlList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                .eq(StringUtils.isNotBlank(sqsxid), SpcySqsxWjjl::getFpid, sqsxid));

        if (CollectionUtils.isNotEmpty(wjjlList)) {
            // 发送专家 函审通知
            List<SpcySqsxWjjl> jcyxphList = wjjlList.stream().filter(x -> StringUtils.isNotBlank(x.getFwjlx1())
                    && x.getFwjlx1().equals(WjmlConstant.hstz)).collect(Collectors.toList());

            if (CollectionUtils.isNotEmpty(jcyxphList)){
                for (SpcySqsxWjjl wjjl : jcyxphList) {
                    // 选派现场审查员的函
                    Result result = this.sendSmsToHcy(sqsxid, wjjl.getFbusid(), wjjl.getId(), wjjl);// 短信
                    if (!result.isSuccess()) {
                        return result;
                    }

                    Result result1 = this.sendEmailToHcy(sqsxid, wjjl.getFbusid(), wjjl.getId(), wjjl, SpcyConstant.HCY_USERTYPE_ZJ);// 邮箱
                    if (!result1.isSuccess()) {
                        return result1;
                    }
                }
            }

            // 发生企业 函 (企业通知书)
            List<SpcySqsxWjjl> qyhList = wjjlList.stream().filter(x -> StringUtils.isNotBlank(x.getFwjlx1())
                    && x.getFwjlx1().equals(WjmlConstant.qytzs)).collect(Collectors.toList());

            if (CollectionUtils.isNotEmpty(qyhList)){
                for (SpcySqsxWjjl wjjl : qyhList) {
                    // 企业单位检查通知
                    Result result = this.sendSmsToQy(sqsxid, wjjl.getId(), wjjl);// 短信
                    if (!result.isSuccess()){
                        return result;
                    }

                    Result result1 = this.sendEmailToQy(sqsxid, wjjl.getId(), wjjl);// 邮箱
                    if (!result1.isSuccess()){
                        return result1;
                    }
                }
            }
        }

        return Result.ok();
    }

    /**
     * 发送短信-给检查员
     */
    public Result sendSmsToHcy(String sqsxId, String xpjlId, String fileId, SpcySqsxWjjl wjjl){
        return this.sendSmsToHcy(sqsxId, xpjlId, null, fileId, wjjl);
    }

    public Result sendSmsToHcy(String sqsxId, String xpjlId, SpcySqsxXpjl xpjl,String fileId, SpcySqsxWjjl wjjl){
        return this.sendSmsToHcy(sqsxId, xpjlId, xpjl, fileId, wjjl, "");
    }

    public Result sendSmsToHcy(String sqsxId, String xpjlId, SpcySqsxXpjl xpjl,String fileId, SpcySqsxWjjl wjjl, String content){
        if (StringUtils.isBlank(sqsxId)){
            return Result.error("申请事项id不能为空");
        }

        // 选派记录
        if (xpjl == null && StringUtils.isNotBlank(xpjlId)) {
            xpjl = spcySqsxXpjlService.getById(xpjlId);
            if (xpjl == null) {
                return Result.error("选派记录不存在，请重试");
            }
        }

        if (StringUtils.isBlank(content)) {
            content = this.getSendLzdxToJcy(sqsxId, null);
        }

        // 查询电话
        String phone = ycSysUserService.findPhoneById(xpjl.getFuserid());

        boolean isSuccess = true; // 发送短信是否成功
        if (StringUtils.isBlank(phone)){
            isSuccess = false;
        }

        String flag = "0";

        try {
            if (isSuccess) {
                flag = smsUtils.sendSms(phone, content);
            }
        } catch (NoSuchAlgorithmException e) {
            log.error("发送短信出错-【无搜索算法异常】- {}",e.getMessage());
        } catch (IOException e) {
            log.error("发送短信出错-【IOException】- {}",e.getMessage());
        } catch (InvalidKeySpecException e) {
            log.error("发送短信出错-【InvalidKeySpecException】- {}",e.getMessage());
        }

        if (!flag.contains(BusinessConstant.SMS_FLAG)) {
            isSuccess = false;
        }

        if (isSuccess){
            // 更新发送检查通知短信
            spcySqsxXpjlService.updateJctzSms(xpjl.getId(), content);

            // 发送日志
//            spcySqsxDxyxfsjlService.initDxyxFsjl(phone, jcyXm, sqsxId, content, SpcyConstant.FILE_TYPE_SMS, isSuccess);
        }

        return Result.ok("发送成功");
    }

    /**
     * 发送短信-给观察员
     */
    public Result sendSmsToGcy(String sqsxId, String fileId, SpcySqsxWjjl wjjl){
        String content = "";
        if (StringUtils.isBlank(sqsxId)){
            return Result.error("申请事项id不能为空");
        }

        // 文件
        if (wjjl == null && StringUtils.isNotBlank(fileId)) {
            wjjl = spcySqsxWjjlService.getById(fileId);
        }

        if (wjjl != null) {
            if (StringUtils.isNotBlank(wjjl.getFdxzt()) && wjjl.getFdxzt().equals(SpcyConstant.WJJL_ZT_DX)) {
                return Result.ok("已发送短信");
            }

            content = wjjl.getFwjnr(); // 发送内容

            content += "\n附件下载地址：" + ClientJumpUrlConstant.Https_pro + ClientJumpUrlConstant.down_wjjl_url+fileId;
            content += "\n        "+DateConversionUtils.DateToYMDHMSChineseString(new Date());
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxId);
        if (sqsx == null){
            return Result.error("当前申请事项不存在，请重试");
        }

        String phone = "";
        String gcyxm = "";
        String fcygcyid = "";
        if (StringUtils.isNotBlank(sqsx.getFcygcyid())) {
            fcygcyid = sqsx.getFcygcyid();
        }

        if (StringUtils.isNotBlank(fcygcyid)){
            SpcyRsGcy gcy = spcyRsGcyService.getById(fcygcyid);
            if (gcy != null){
                gcyxm = gcy.getFgcyxm();
                phone = gcy.getFphone();
            }
        }

        boolean isSuccess = true; // 发送短信是否成功
        if (StringUtils.isBlank(phone)){
//            return Result.error("当前【"+gcyxm+"】观察员的电话不存在，请完善电话信息");
            isSuccess = false;
        }

        String flag = "0";

        try {
            if (isSuccess) {
                flag = smsUtils.sendSms(phone, content);
            }

        } catch (NoSuchAlgorithmException e) {
            log.error("发送短信出错-【无搜索算法异常】- {}",e.getMessage());
        } catch (IOException e) {
            log.error("发送短信出错-【IOException】- {}",e.getMessage());
        } catch (InvalidKeySpecException e) {
            log.error("发送短信出错-【InvalidKeySpecException】- {}",e.getMessage());
        }

        if (flag.contains(BusinessConstant.SMS_FLAG)) {
            // 更新发送短信
            spcySqsxWjjlService.updateWjjlByFdxzt(fileId);

        }else {
            isSuccess = false;
        }

        if (isSuccess){
            // 发送日志
            spcySqsxDxyxfsjlService.initDxyxFsjl(phone, gcyxm, sqsxId, content, SpcyConstant.FILE_TYPE_SMS, isSuccess);
        }

        return Result.ok("发送成功");
    }

    /**
     * 发送短信-给企业
     */
    public Result sendSmsToQy(String sqsxId, String fileId, SpcySqsxWjjl wjjl){
        String content = "";
        if (StringUtils.isBlank(sqsxId)){
            return Result.error("申请事项id不能为空");
        }

        // 文件
        if (wjjl == null && StringUtils.isNotBlank(fileId)) {
            wjjl = spcySqsxWjjlService.getById(fileId);
        }

        if (wjjl != null) {
            if (StringUtils.isNotBlank(wjjl.getFdxzt()) && wjjl.getFdxzt().equals(SpcyConstant.WJJL_ZT_DX)) {
                return Result.ok("已发送短信");
            }

            /*content = wjjl.getFwjnr(); // 发送内容
            content += "\n附件下载地址：" + ClientJumpUrlConstant.Https_pro + ClientJumpUrlConstant.down_wjjl_url+fileId;
            content += "\n        "+DateConversionUtils.DateToYMDHMSChineseString(new Date());*/
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxId);
        if (sqsx == null){
            return Result.error("当前申请事项不存在，请重试");
        }

        content = this.getSendLzdxToQy(sqsxId, sqsx); // 发送廉政短信

        String phone = "";
        String qymc = sqsx.getFqymc();

        /*String qyid = "";
        if (StringUtils.isNotBlank(sqsx.getFqyid())) {
            qyid = sqsx.getFqyid();
        }

        if (StringUtils.isNotBlank(qyid)){
            SpcyQyJbxx qyxx = spcyQyJbxxService.getById(qyid);
            if (qyxx != null){
                qymc = qyxx.getFqymc();
                phone = qyxx.getFqylxrdh();
            }
        }*/

        boolean isSuccess = true; // 发送短信是否成功
        // 获取企业联系人
        XzspXzxdrJbxxVo xzspXzxdrJbxxVo = xzXkMapper.selectQylxrxgxxBySqsx(sqsx.getFsxsqjbxxid());
        if (xzspXzxdrJbxxVo == null){
            isSuccess = false;

        }else {
            if (org.apache.commons.lang3.StringUtils.isNotBlank(xzspXzxdrJbxxVo.getLxdh())) {
                phone = xzspXzxdrJbxxVo.getLxdh();

            } else if (org.apache.commons.lang3.StringUtils.isNotBlank(xzspXzxdrJbxxVo.getLxrsj())) {
                phone = xzspXzxdrJbxxVo.getLxrsj();
            }
        }

        if (StringUtils.isBlank(phone)){
//            return Result.error("当前【"+qymc+"】企业的电话不存在，请完善电话信息");
            isSuccess = false;
        }

        String flag = "0";

        try {
            if (isSuccess) {
                flag = smsUtils.sendSms(phone, content);
            }

        } catch (NoSuchAlgorithmException e) {
            log.error("发送短信出错-【无搜索算法异常】- {}",e.getMessage());
        } catch (IOException e) {
            log.error("发送短信出错-【IOException】- {}",e.getMessage());
        } catch (InvalidKeySpecException e) {
            log.error("发送短信出错-【InvalidKeySpecException】- {}",e.getMessage());
        }

        if (flag.contains(BusinessConstant.SMS_FLAG)) {
            // 更新发送短信
            spcySqsxWjjlService.updateWjjlByFdxzt(fileId);

        }else {
            isSuccess = false;
        }

        if (isSuccess){
            // 发送日志
            spcySqsxDxyxfsjlService.initDxyxFsjl(phone, qymc, sqsxId, content, SpcyConstant.FILE_TYPE_SMS, isSuccess);
        }

        return Result.ok("发送成功");
    }

    /**
     * 发送短信-给药品注册处
     * @param fileId
     * @return
     */
    public Result sendSmsToYpzcCs(String sqsxId,String fileId, SpcySqsxWjjl wjjl){
        if (StringUtils.isBlank(sqsxId)){
            return Result.error("申请事项id不能为空");
        }
        if (StringUtils.isBlank(fileId)){
            return Result.error("文件id不能为空");
        }

        SpcySqsxBasic basic = spcySqsxBaiscService.selectSqsxById(sqsxId);
        if (basic == null){
            return Result.error("当前申请事项不存在，请重试");
        }

        boolean isSuccess = true; // 发送成功状态
        // 查询处室账号
        List<YcSysUser> userList = ycSysUserService.list(new LambdaQueryWrapper<YcSysUser>()
                .like(YcSysUser::getOrgName, SpcyConstant.getOrgNameInCsByYwflcode(basic.getFywflcode())));
        if (CollectionUtils.isEmpty(userList)){
            isSuccess = false;
        }

        String phone = "";
        String name = "";
        String content = "";

        if (isSuccess) {
            for (YcSysUser user : userList) {
                if (StringUtils.isNotBlank(user.getPhone())){
                    phone = user.getPhone();
                    name = user.getOrgName() + "-" + user.getRealname();
                    break;
                }
            }
        }

        if (StringUtils.isBlank(phone)){
            isSuccess = false;
        }

        // 文件
        if (wjjl == null) {
            wjjl = spcySqsxWjjlService.getById(fileId);
            if (wjjl == null) {
                return Result.error("文件记录不存在，请重试");
            }
        }

        content = wjjl.getFwjnr(); // 发送内容
        content += "\n附件下载地址：" + ClientJumpUrlConstant.Https_pro + ClientJumpUrlConstant.down_wjjl_url+fileId;
        content += "\n        "+DateConversionUtils.DateToYMDHMSChineseString(new Date());

        String flag = "0";

        try {
            if (isSuccess) {
                flag = smsUtils.sendSms(phone, content);
            }

        } catch (NoSuchAlgorithmException e) {
            log.error("发送短信出错-【无搜索算法异常】- {}",e.getMessage());
        } catch (IOException e) {
            log.error("发送短信出错-【IOException】- {}",e.getMessage());
        } catch (InvalidKeySpecException e) {
            log.error("发送短信出错-【InvalidKeySpecException】- {}",e.getMessage());
        }

        if (flag.contains(BusinessConstant.SMS_FLAG)) {
            // 更新发送短信
            spcySqsxWjjlService.updateWjjlByFdxzt(wjjl.getId());

        }else {
            isSuccess = false;
        }

        if (isSuccess){
            // 发送日志
            spcySqsxDxyxfsjlService.initDxyxFsjl(phone, name, sqsxId, content, SpcyConstant.FILE_TYPE_SMS, isSuccess);
        }

        return Result.ok("发送成功");

    }

    /** -------------------------------------------【 发送邮箱 - 业务功能 】--------------------------------------------------------- */

    /**
     * 发送邮箱-给检查员
     */
    public Result sendEmailToHcy(String sqsxId,String xpjlId,String fileId, SpcySqsxWjjl wjjl, String flowType){

        String sendTo = ""; // 邮箱接收人邮箱
        String title = "关于拟选派"+flowType+"的函";
        String content = "";
        String fileName = ""; // 文件名
        String fileURL = ""; // 文件地址

        if (StringUtils.isBlank(sqsxId)){
            return Result.error("申请事项id不能为空");
        }

        if (StringUtils.isBlank(xpjlId)){
            return Result.error("选派记录id不能为空");
        }

        if (StringUtils.isBlank(fileId)){
            return Result.error("文件id不能为空");
        }

        // 文件
        if (wjjl == null) {
            wjjl = spcySqsxWjjlService.getById(fileId);
            if (wjjl == null) {
                return Result.error("文件记录不存在，请重试");
            }
        }

        content = wjjl.getFwjnr(); // 文件内容
        fileName = wjjl.getFwjxsmc()+wjjl.getFwjlx(); // 文件名
        fileURL = wjjl.getFcflj();

        // 选派记录
        SpcySqsxXpjl xpjl = spcySqsxXpjlService.getById(xpjlId);
        if (xpjl == null){
            return Result.error("选派记录不存在，请重试");
        }

        String jcyXm = "";

        YcSysUser user = ycSysUserService.getById(xpjl.getFuserid());
        if (user != null){
            jcyXm = user.getRealname();
            sendTo = user.getEmail();
        }

        boolean isSuccess = true; // 发送成功状态
        if (StringUtils.isBlank(sendTo)){
//            return Result.error("当前【"+jcyXm+"】"+flowType+"的邮箱为空,请完善邮箱信息");
            isSuccess = false;
        }

        if (!CheckEmailUtils.checkEmail(sendTo)){
//            return Result.error("邮箱格式不正确");
            isSuccess = false;
        }

        // 发送邮箱带附件
        if (isSuccess) {
            sendMailUtils.sendMail(title, content, sendTo, fileName, fileURL, localFileUploadUtils.isEnableMinioWjjlFile());
        }

        // 发送日志
        spcySqsxDxyxfsjlService.initDxyxFsjl(sendTo, jcyXm, sqsxId, content, SpcyConstant.FILE__TYPE_MAIL, isSuccess);

        // 更新发送邮箱
        spcySqsxWjjlService.updateWjjlByFyxzt(wjjl.getId());

        return Result.ok("发送成功");
    }

    /**
     * 发送邮箱-给观察员
     */
    public Result sendEmailToGcy(String sqsxId,String fileId,  SpcySqsxWjjl wjjl){
        String sendTo = ""; // 邮箱接收人邮箱
        String title = "关于" + WjmlConstant.xpxcgcydh;
        String content = "";
        String fileName = ""; // 文件名
        String fileURL = ""; // 文件地址

        if (StringUtils.isBlank(sqsxId)){
            return Result.error("申请事项id不能为空");
        }

        if (StringUtils.isBlank(fileId)){
            return Result.error("文件id不能为空");
        }

        // 文件
        if (wjjl == null) {
            wjjl = spcySqsxWjjlService.getById(fileId);
            if (wjjl == null) {
                return Result.error("文件记录不存在，请重试");
            }
        }

        if (StringUtils.isNotBlank(wjjl.getFyxzt()) && wjjl.getFyxzt().equals(SpcyConstant.WJJL_ZT_YX)){
            return Result.ok("已发送邮箱");
        }

        content = wjjl.getFwjnr();
        fileName = wjjl.getFwjxsmc()+wjjl.getFwjlx();
        fileURL = wjjl.getFcflj();

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxId);
        if (sqsx == null){
            return Result.error("当前申请事项不存在，请重试");
        }

        String gcyxm = "";
        String fcygcyid = "";
        if (StringUtils.isNotBlank(sqsx.getFcygcyid())) {
            fcygcyid = sqsx.getFcygcyid();
        }

        if (StringUtils.isNotBlank(fcygcyid)){
            SpcyRsGcy gcy = spcyRsGcyService.getById(fcygcyid);
            if (gcy != null){
                gcyxm = gcy.getFgcyxm();
                sendTo = gcy.getFemail();
            }
        }

        boolean isSuccess = true; // 发送成功状态
        if (StringUtils.isBlank(sendTo)){
//            return Result.error("当前【"+gcyxm+"】观察员的邮箱不存在，请完善邮箱信息");
            isSuccess = false;
        }

        if (!CheckEmailUtils.checkEmail(sendTo)){
//            return Result.error("邮箱格式不正确");
            isSuccess =  false;
        }

        // 发送邮箱带附件
        if (isSuccess) {
            sendMailUtils.sendMail(title, content, sendTo, fileName, fileURL, localFileUploadUtils.isEnableMinioWjjlFile());
        }

        // 发送日志
        spcySqsxDxyxfsjlService.initDxyxFsjl(sendTo, gcyxm, sqsxId, content, SpcyConstant.FILE__TYPE_MAIL, isSuccess);

        // 更新发送邮箱
        spcySqsxWjjlService.updateWjjlByFyxzt(wjjl.getId());

        return Result.ok("发送成功");
    }

    /**
     * 发送邮箱-给企业 （审评查验流程）
     */
    public Result sendEmailToQy(String sqsxId,String fileId, SpcySqsxWjjl wjjl){
        String sendTo = ""; // 邮箱接收人邮箱
        String title = "关于" + WjmlConstant.qydwjctz;
        String content = "";
        String fileName = ""; // 文件名
        String fileURL = ""; // 文件地址

        if (StringUtils.isBlank(sqsxId)){
            return Result.error("申请事项id不能为空");
        }
        if (StringUtils.isBlank(fileId)){
            return Result.error("文件id不能为空");
        }
        // 文件
        if (wjjl == null) {
            wjjl = spcySqsxWjjlService.getById(fileId);
            if (wjjl == null) {
                return Result.error("文件记录不存在，请重试");
            }
        }

        if (StringUtils.isNotBlank(wjjl.getFyxzt()) && wjjl.getFyxzt().equals(SpcyConstant.WJJL_ZT_YX)){
            return Result.ok("已发送邮箱");
        }

        fileName = wjjl.getFwjxsmc()+wjjl.getFwjlx();
        fileURL = wjjl.getFcflj();

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxId);
        if (sqsx == null){
            return Result.error("当前申请事项不存在，请重试");
        }

        String qymc = sqsx.getFqymc();

        boolean isSuccess = true; // 发送短信是否成功
        // 获取企业联系人
        XzspXzxdrJbxxVo xzspXzxdrJbxxVo = xzXkMapper.selectQylxrxgxxBySqsx(sqsx.getFsxsqjbxxid());
        if (xzspXzxdrJbxxVo == null){
            isSuccess = false;

        }else {
            if (org.apache.commons.lang3.StringUtils.isNotBlank(xzspXzxdrJbxxVo.getDzyx())) {
                sendTo = xzspXzxdrJbxxVo.getDzyx();

            } else if (org.apache.commons.lang3.StringUtils.isNotBlank(xzspXzxdrJbxxVo.getDzyj())) {
                sendTo = xzspXzxdrJbxxVo.getDzyj();
            }
        }

        if (StringUtils.isBlank(sendTo)){
            isSuccess = false;
        }

        if (!CheckEmailUtils.checkEmail(sendTo)){
            isSuccess = false;
        }

        // 发送邮箱带附件
        if (isSuccess) {
            sendMailUtils.sendMail(title, content, sendTo, fileName, fileURL, localFileUploadUtils.isEnableMinioWjjlFile());
        }

        // 发送日志
        spcySqsxDxyxfsjlService.initDxyxFsjl(sendTo, qymc, sqsxId, content, SpcyConstant.FILE__TYPE_MAIL, isSuccess);

        // 更新发送邮箱
        spcySqsxWjjlService.updateWjjlByFyxzt(wjjl.getId());

        return Result.ok("发送成功");
    }

    /**
     * 发生邮箱-给企业 （回访任务）
     */
    public Result sendEmailToQyInHfrw(String sqsxId,SpcySqsxBasic sqsx,SpcySqsxWjjl wjjl,boolean isMinio){

        String sendTo = ""; // 邮箱接收人邮箱
        String title = wjjl.getFwjxsmc();
        String content = "";
        String fileName = ""; // 文件名
        String fileURL = ""; // 文件地址

        if (StringUtils.isBlank(sqsxId)){
            return Result.error("申请事项id不能为空");
        }

        fileName = wjjl.getFwjxsmc()+wjjl.getFwjlx();
        fileURL = wjjl.getFcflj();

        String qymc = "";
        String qyid = "";
        if (StringUtils.isNotBlank(sqsx.getFqyid()) && StringUtils.isNotBlank(sqsx.getFqyid())) {
            qyid = sqsx.getFqyid();
        }

        if (StringUtils.isNotBlank(qyid)){
            SpcyQyJbxx qyxx = spcyQyJbxxService.getById(qyid);
            if (qyxx != null){
                qymc = qyxx.getFqymc();
                sendTo = qyxx.getFqylxryx();
            }
        }

        if (StringUtils.isBlank(sendTo)){
            return Result.error("当前【"+qymc+"】企业的邮箱不存在，请完善邮箱信息");
        }

        if (!CheckEmailUtils.checkEmail(sendTo)){
            return Result.error("邮箱格式不正确");
        }

        // 发送邮箱带附件
        sendMailUtils.sendMail(title, content, sendTo, fileName,fileURL,isMinio);

        // 发送日志
        /*SpcySqsxDxyxfsjl spcySqsxDxyxfsjl = new SpcySqsxDxyxfsjl();
        spcySqsxDxyxfsjl.setId(GetUuIdUtils.ReplaceUuId());
        spcySqsxDxyxfsjl.setFywhm(sendTo);
        spcySqsxDxyxfsjl.setFname(qymc);
        spcySqsxDxyxfsjl.setFsqsxid(sqsxId);
        spcySqsxDxyxfsjl.setFfsnr(content);
        spcySqsxDxyxfsjl.setFfsr(nowUsername);
        spcySqsxDxyxfsjl.setFfssj(new Date());
        spcySqsxDxyxfsjl.setFtype1(SpcyConstant.FILE__TYPE_MAIL); // 邮箱
        spcySqsxDxyxfsjlService.save(spcySqsxDxyxfsjl);*/

        // 日志
//        addSendWjjlLog(sqsxId, true, SpcyConstant.FILE_TYPE_SMS, qymc, sendTo,content,null);

        return Result.ok("发送成功");

    }

    /**
     * 发送邮箱给药品注册处
     * @param fileId
     * @return
     */
    public Result sendEmailToYpzcCs(String sqsxId,String fileId, SpcySqsxWjjl wjjl){

        String name = "";
        String sendTo = ""; // 邮箱接收人邮箱
        String title = "";
        String content = "";
        String fileName = ""; // 文件名
        String fileURL = ""; // 文件地址

        if (StringUtils.isBlank(sqsxId)){
            return Result.error("申请事项id不能为空");
        }
        if (StringUtils.isBlank(fileId)){
            return Result.error("文件id不能为空");
        }
        // 文件
        if (wjjl == null) {
            wjjl = spcySqsxWjjlService.getById(fileId);
            if (wjjl == null) {
                return Result.error("文件记录不存在，请重试");
            }
        }

        if (StringUtils.isNotBlank(wjjl.getFyxzt()) && wjjl.getFyxzt().equals(SpcyConstant.WJJL_ZT_YX)){
            return Result.ok("已发送邮箱");
        }

        SpcySqsxBasic basic = spcySqsxBaiscService.selectSqsxById(sqsxId);
        if (basic == null){
            return Result.error("当前申请事项不存在，请重试");
        }

        // 查询处室账号
        boolean isSuccess = true; // 发送成功状态
        List<YcSysUser> userList = ycSysUserService.list(new LambdaQueryWrapper<YcSysUser>()
                .like(YcSysUser::getOrgName, SpcyConstant.getOrgNameInCsByYwflcode(basic.getFywflcode())));
        if (CollectionUtils.isEmpty(userList)){
            isSuccess = false;
        }

        if (isSuccess) {
            for (YcSysUser user : userList) {
                if (StringUtils.isNotBlank(user.getEmail())){
                    sendTo = user.getEmail();
                    name = user.getOrgName() + "-" + user.getRealname();
                    break;
                }
            }
        }

        fileName = wjjl.getFwjxsmc()+wjjl.getFwjlx();
        fileURL = wjjl.getFcflj();


        if (StringUtils.isBlank(sendTo)){
            isSuccess = false;
        }

        if (!CheckEmailUtils.checkEmail(sendTo)){
            isSuccess = false;
        }

        // 发送邮箱带附件
        if (isSuccess) {
            sendMailUtils.sendMail(title, content, sendTo, fileName, fileURL, localFileUploadUtils.isEnableMinioWjjlFile());
        }

        // 发送日志
        spcySqsxDxyxfsjlService.initDxyxFsjl(sendTo, name, sqsxId, content, SpcyConstant.FILE__TYPE_MAIL, isSuccess);

        // 更新发送邮箱
        spcySqsxWjjlService.updateWjjlByFyxzt(wjjl.getId());

        return Result.ok("发送成功");
    }

    /**
     * 廉政温馨提示（企业）(1)
     */
    public String getSendLzdxToQy(String sqsxid, SpcySqsxBasic sqsx){
        if (sqsx == null){
            sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        }
        String qymc = sqsx.getFqymc(); // 企业
        String fzr = ""; // 负责人
        String kssj = ""; // 中文日期
        String jssj = "";

        if (sqsx.getFcykssj() != null) {
            kssj = DateConversionUtils.DateToYMDString(DateConversionUtils.DateToString(sqsx.getFcykssj()));
        }
        if (sqsx.getFcyjssj() != null) {
            jssj = DateConversionUtils.DateToYMDString(DateConversionUtils.DateToString(sqsx.getFcyjssj()));
        }

        String msg = "\n[通知提醒]\n";
        msg += "尊敬的" + qymc;
        msg += "负责人" + fzr + ": " + "\n";
        msg += "  ";
        msg += kssj + "至" + jssj + "，";
        msg += "我中心派出检查组对贵公司进行现场检查。请贵公司在接受现场检查期间中自觉维护检查工作的公正性和严肃性，" +
                "严格遵守本中心现场检查纪律要求，共同执行廉洁自律规范。检查期间无须承担任何费用。";
        msg += "\n";
        msg += "  感谢您对中心工作的支持，祝您工作顺利！";
        msg += "\n\n";
        msg += "    广西壮族自治区食品药品审评查验中心";

        return msg;
    }

    /**
     * 廉政温馨提示（检查员）0519
     */
    public String getSendLzdxToJcy(String sqsxid, SpcySqsxBasic sqsx){

        String msg = "\n[通知提醒]\n";
        msg += "尊敬的检查员：";
        msg += "\n";
        msg += "  欢迎您参加此次检查工作！请您在现场检查过程中自觉维护检查工作的公正性和严肃性，" +
                "严格执行自治区药监局药品现场检查人员廉洁自律规范，严格遵守本中心现场检查纪律。";
        msg += "\n";
        msg += "  感谢您对中心工作的支持，祝您工作顺利！";
        msg += "\n\n";
        msg += "    广西壮族自治区食品药品审评查验中心";

        return msg;
    }

    /** -------------------------------------------【 发送短信和邮箱日志记录 - 功能 】--------------------------------------------------------- */

    /**
     * 发送短信、邮箱操作记录  // 待完善
     * @param spcyId 审评查验id
     * @param sendFlag 发送标准
     * @param type 类型
     * @param name 满足
     * @param hm 号码
     */
    public void addSendWjjlLog(String spcyId,boolean sendFlag,String type,String name,String hm,String content,String fileId){
        String OpCon = "";
        if (type.equals(SpcyConstant.FILE_TYPE_SMS)){
            OpCon = "【发送短信】给["+name+"];【号码】为：["+hm+"];\n【发送内容】为: ["+content+"]";
        }
        if (type.equals(SpcyConstant.FILE__TYPE_MAIL)){
            SpcySqsxWjjl wjjl = spcySqsxWjjlService.getById(fileId);
            if (wjjl != null && org.apache.commons.lang3.StringUtils.isNotBlank(wjjl.getFwjxsmc()) && org.apache.commons.lang3.StringUtils.isNotBlank(wjjl.getFwjxsmc())) {
                OpCon = "【发送邮箱】给[" + name + "];【邮箱号】为：[" + hm + "];\n【文件编号】为[" + fileId + "];" +
                        "【文件显示名称】为["+wjjl.getFwjxsmc()+"];\n【文件存储名称】为["+wjjl.getFwjxsmc()+"];" +
                        "【文件类型】为["+wjjl.getFwjlx()+"];";
            }else {
                OpCon = "【发送邮箱】给[" + name + "];【邮箱号】为：[" + hm + "];【文件编号】为[" + fileId + "];";
            }
            if (org.apache.commons.lang3.StringUtils.isNotBlank(content)){
                OpCon += "\n发送内容["+content+"];";
            }
        }
        if (sendFlag) {
            spcyLogService.addLogs(YpscFlowConstant.HJMC_FAZZ, OpCon, spcyId, type, SpcyConstant.SMS_STATE_TYPE1);
        } else {
            spcyLogService.addLogs(YpscFlowConstant.HJMC_FAZZ, OpCon, spcyId, type, SpcyConstant.SMS_STATE_TYPE2);
        }

    }

    /** -------------------------------------------【 查询申请事项相关内容 - 功能 】--------------------------------------------------------- */

    /**
     * 获取查验时间、或者审评时间
     */
    public Map getSqsxCyOrSpsj(SpcySqsxBasic sqsx,String flowType){
        Map map = new HashMap();
        Date kssj = null;
        Date jssj = null;
        if (sqsx != null){
            if (flowType.equals(SpcyConstant.FLOW_TYPE_CY)){
                kssj = sqsx.getFcykssj();
                jssj = sqsx.getFcyjssj();
            }else if (flowType.equals(SpcyConstant.FLOW_TYPE_ZG)){
                kssj = sqsx.getFcykssj();
                jssj = sqsx.getFcyjssj();
            }else {
                kssj = sqsx.getFspkssj();
                jssj = sqsx.getFspjssj();
            }
        }
        map.put("kssj",kssj);
        map.put("jssj",jssj);
        return map;
    }

    /**
     * 获取已选派人员的数量及状态-根据检查身份-判断当前是否允许发送短信
     */
    public SqsxXpryNumVo getYxprySlhzt(String sqsxid){
        SqsxXpryNumVo resultVo = new SqsxXpryNumVo();
        SqsxZzZyYbNumVo sqsxNum = spcySqsxBaiscService.selectZzZyYbNumBySqsxId(sqsxid);
        if (sqsxNum != null){
            resultVo.setOrgFzzrs(Optional.ofNullable(sqsxNum.getFzzrs()).orElseGet(()->0));
            resultVo.setOrgFzyrs(Optional.ofNullable(sqsxNum.getFzyrs()).orElseGet(()->0));
            resultVo.setOrgFybrs(Optional.ofNullable(sqsxNum.getFybrs()).orElseGet(()->0));
        }

        // 现在选派的数量
        SqsxZzZyYbNumVo xpNum = spcySqsxXpjlService.getXpWqrFsdqrSmsZzZyYbNumBean(sqsxid);

        resultVo.setXpFzzrs(xpNum.getFzzrs());
        resultVo.setXpFzyrs(xpNum.getFzyrs());
        resultVo.setXpFybrs(xpNum.getFybrs());

        // true: 可以发送短信、选派人员
        resultVo.setZzFlag(resultVo.getOrgFzzrs() > xpNum.getFzzrs() ? true : false);
        resultVo.setZyFlag(resultVo.getOrgFzyrs() > xpNum.getFzyrs() ? true : false);
        resultVo.setYbFlag(resultVo.getOrgFybrs() > xpNum.getFybrs() ? true : false);


        return resultVo;
    }

    /**
     * 验证当前选派记录-是否能够发送短信(选派人员数量是否已经满足事项)
     */
    public boolean verifySfSendSmsByFssk(SqsxXpryNumVo numVo, String sqsxid, String fssk){
        if (numVo == null){
            // 获取组长组员现在数量及确认状态
            numVo = this.getYxprySlhzt(sqsxid);
        }

        if (fssk.equals(SpcyConstant.HCY_SSK_ZZ)){
            return numVo.isZzFlag();
        }else if (fssk.equals(SpcyConstant.HCY_SSK_ZY)){
            return numVo.isZyFlag();
        }else if (fssk.equals(SpcyConstant.HCY_SSK_YB)){
            return numVo.isYbFlag();
        }

        return false;
    }

    /**
     * 获取当前申请事项-分类编码
     */
    public String getSqsxCodeBySqsxId(String sqsxid){
        return spcySqsxBaiscService.getYwflCodeBySqsxId(sqsxid);
    }

    public SpcySqsxBasic getSqsxInfoBySqsxId(String sqsxid){
        if (StringUtils.isBlank(sqsxid)){
            return new SpcySqsxBasic();
        }
        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxInfoById(sqsxid);
        if (sqsx != null){
            return sqsx;
        }
        return new SpcySqsxBasic();
    }


    public SpcySqsxBasic getSqsxBySqsxId(String sqsxid){
        if (StringUtils.isBlank(sqsxid)){
            return new SpcySqsxBasic();
        }
        SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(sqsxid);
        if (sqsx != null){
            return sqsx;
        }
        return new SpcySqsxBasic();
    }

    /**
     * 获取当前流程环节名称
     * @param sqsxId 申请事项id
     * @return
     */
    public String getNowNodeName(String sqsxId){
        String ywflCode = getYwflCode(sqsxId);
        List<String> nodeList = new ArrayList<>();
        if (StringUtils.isBlank(ywflCode)){
            return "";
        }
        if (Objects.equals(ywflCode, SpcyConstant.YWFL_CODE_YPSC)) {
            nodeList = Arrays.asList(YpscFlowConstant.NODENAME.split(","));
        }else if (Objects.equals(ywflCode, SpcyConstant.YWFL_CODE_YPZC)) {
            nodeList = Arrays.asList(YpzcFlowConstant.NODENAME.split(","));
        }else if (Objects.equals(ywflCode, SpcyConstant.YWFL_CODE_YPJY)) {
            nodeList = Arrays.asList(YpltFlowConstant.NODENAME.split(","));
        }else if (Objects.equals(ywflCode, SpcyConstant.YWFL_CODE_YLQX)) {
            nodeList = Arrays.asList(YlqxFlowConstant.NODENAME.split(","));
        }else if (Objects.equals(ywflCode, SpcyConstant.YWFL_CODE_HZP)) {
            nodeList = Arrays.asList(HzpFlowConstant.NODENAME.split(","));
        }else if (Objects.equals(ywflCode, SpcyConstant.YWFL_CODE_YPZCLC)) {
            nodeList = Arrays.asList(YpzclcFlowConstant.NODENAME.split(","));
        }

        if (StringUtils.isBlank(sqsxId)){
            return "";
        }
        // 当前流转待办
        List<SpcySqsxJob> sqsxJob = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                .eq(SpcySqsxJob::getFpid, sqsxId)
                .in(CollectionUtils.isNotEmpty(nodeList),SpcySqsxJob::getFhjmc,nodeList)
                .eq(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_DB));
        if (CollectionUtils.isNotEmpty(sqsxJob)){
            return sqsxJob.get(0).getFhjmc();
        }

        return "";
    }

    /**
     * 获取申请事项的业务分类编码
     * @param sqsxId 申请事项id
     * @return
     */
    public String getYwflCode(String sqsxId){
        return spcySqsxBaiscService.getYwflCodeBySqsxId(sqsxId);
    }

    /**
     * 获取当前流程任务环节名称
     * @param jobid 流程任务jobid
     */
    public String getJobNodeName(String jobid){
        SpcySqsxJob job = spcySqsxJobService.getById(jobid);
        if (job != null && StringUtils.isNotBlank(job.getFhjmc())){
            return job.getFhjmc();
        }
        return "";
    }

    /**
     * 获取申请事项-相关文件
     */
    public List<SpcySqsxWjjl> getWjjlListBySqsxId(String sqsxid){
        List<SpcySqsxWjjl> wjjlList = new ArrayList<>();
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid, sqsxid);
        queryWrapper.orderByDesc(SpcySqsxWjjl::getCreateTime);
        wjjlList = spcySqsxWjjlService.list(queryWrapper);

        return wjjlList;
    }

    /**
     * 获取申请事项-流转记录
     */
    public List<SpcySqsxJob> getJobListBySqsxId(String sqsxid){
        LambdaQueryWrapper<SpcySqsxJob> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(SpcySqsxJob::getFlcmc, YpscFlowConstant.LCMC_LIKE_SPCYLC);
        queryWrapper.eq(SpcySqsxJob::getFpid, sqsxid);
        queryWrapper.orderByAsc(SpcySqsxJob::getFfssj);
        queryWrapper.orderByAsc(SpcySqsxJob::getCreateTime);
        List<SpcySqsxJob> jobList = spcySqsxJobService.list(queryWrapper);

        return jobList;
    }

    /**
     * 获取申请事项-流转记录-未推送记录
     */
    public List<SpcySqsxJob> getWtsJobListBySqsxId(String sqsxid){
        LambdaQueryWrapper<SpcySqsxJob> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxJob::getFpid, sqsxid);
        queryWrapper.eq(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_YB);
        queryWrapper.isNull(SpcySqsxJob::getFtszt);
        queryWrapper.like(SpcySqsxJob::getFlcmc, SpcyFlowConstant.LCMC_LIKE_SPCYLC);
        queryWrapper.orderByAsc(SpcySqsxJob::getFfssj);
        List<SpcySqsxJob> jobList = spcySqsxJobService.list(queryWrapper);

        return jobList;
    }

    /** 将未推送流程记录 改为 已推送状态 */
    public void updateWtsJobList(String sqsxid){
        LambdaUpdateWrapper<SpcySqsxJob> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.like(SpcySqsxJob::getFlcmc, SpcyFlowConstant.LCMC_LIKE_SPCYLC);
        updateWrapper.eq(SpcySqsxJob::getFpid, sqsxid);
        updateWrapper.isNull(SpcySqsxJob::getFtszt);

        SpcySqsxJob updateJob = new SpcySqsxJob();
        updateJob.setFtszt("是");
        spcySqsxJobService.update(updateJob, updateWrapper);
    }

    /** 将未推送流程记录 改为 已推送状态 */
    public void updateWtsJobList(List<SpcySqsxJob> jobList){
        for (SpcySqsxJob job : jobList) {
            LambdaUpdateWrapper<SpcySqsxJob> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcySqsxJob::getId, job.getId());

            SpcySqsxJob updateJob = new SpcySqsxJob();
            updateJob.setFtszt("是");
            spcySqsxJobService.update(updateJob, updateWrapper);
        }
    }

    /**
     * 获取申请事项-检查项目
     */
    public List<SpcySqsxJcxm> getJcxmListBySqsxId(String sqsxid){
        LambdaQueryWrapper<SpcySqsxJcxm> jcxmQueryWrapper = new LambdaQueryWrapper<>();
        jcxmQueryWrapper.eq(SpcySqsxJcxm::getFsqsxid, sqsxid);
        jcxmQueryWrapper.orderByAsc(SpcySqsxJcxm::getFtype1);
        jcxmQueryWrapper.orderByAsc(SpcySqsxJcxm::getFtype2);
        jcxmQueryWrapper.orderByAsc(SpcySqsxJcxm::getFtype3);
        jcxmQueryWrapper.orderByAsc(SpcySqsxJcxm::getFtype4);
        jcxmQueryWrapper.orderByAsc(SpcySqsxJcxm::getFxh);
        List<SpcySqsxJcxm> jcxmList = spcySqsxJcxmService.list(jcxmQueryWrapper);

        return jcxmList;
    }

    /**
     * 获取申请事项-检查项目-有缺陷的记录
     */
    public List<SpcySqsxJcxm> getJcxmQxListBySqsxId(SpcySqsxBasic sqsx){
        if (sqsx == null){
            return new ArrayList<SpcySqsxJcxm>();
        }
        String jcjl = SpcyConstant.getJcxmDefaultScjl(sqsx.getFywflcode());

        LambdaQueryWrapper<SpcySqsxJcxm> jcxmQueryWrapper = new LambdaQueryWrapper<>();
        jcxmQueryWrapper.eq(SpcySqsxJcxm::getFsqsxid, sqsx.getId());
        jcxmQueryWrapper.ne(StringUtils.isNotBlank(jcjl),SpcySqsxJcxm::getFjcjl,jcjl); // 不等于无缺陷
        jcxmQueryWrapper.orderByAsc(SpcySqsxJcxm::getFxh);
        List<SpcySqsxJcxm> jcxmList = spcySqsxJcxmService.list(jcxmQueryWrapper);

        return jcxmList;
    }

    /**
     * 获取申请事项-选派记录
     * @param sqsxid 申请事项id
     * @param flowType 流程类型（查验、审评）
     */
    public List<SpcySqsxXpjl> getXpjlBySqsxId(String sqsxid,String flowType){
        return spcySqsxXpjlService.getXpjlBySqsxId(sqsxid,flowType);
    }

    /**
     * 获取同意-申请事项选派记录
     */
    public List<SpcySqsxXpjl> getTyXpXpjlList(String sqsxid,String flowType){
        return spcySqsxXpjlService.getTyXpXpjlList(sqsxid, flowType);
    }

    /**
     * 获取初始化文件数据 - 查验 - 现场检查环节
     * @param sqsxid 申请事项id
     */
    public Map getInitXcjcWjjlList(String sqsxid){
        Map map = new HashMap();

        // 获取申请事项-相关文件(不属于行政审评的数据)
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid,sqsxid);
        List<SpcySqsxWjjl> wjjlList = spcySqsxWjjlService.list(queryWrapper);

        List<SpcySqsxWjjl> jcfaList = new ArrayList<>(); // 检查方案
        jcfaList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.jcfa)).collect(Collectors.toList());
        map.put("jcfaList",jcfaList); // 检查方案

        // 现场检查环节
        List<SpcySqsxWjjl> wlyctsmList = new ArrayList<>(); // 无利益冲突声明
        wlyctsmList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.wlyctsm)).collect(Collectors.toList());
        map.put("wlyctsmList",wlyctsmList);

        List<SpcySqsxWjjl> jcycnsList = new ArrayList<>(); // 现场检查检查员承诺书
        jcycnsList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.xcjcjcycns)).collect(Collectors.toList());
        map.put("jcycnsList",jcycnsList);

        List<SpcySqsxWjjl> xcjcjlList = new ArrayList<>(); // 现场检查纪律
        xcjcjlList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.xcjcjl)).collect(Collectors.toList());
        map.put("xcjcjlList",xcjcjlList);

        List<SpcySqsxWjjl> hyqdbList = new ArrayList<>(); // 现场检查会议签到表
        hyqdbList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.xcjchyqdb)).collect(Collectors.toList());
        map.put("hyqdbList",hyqdbList);

        List<SpcySqsxWjjl> ljzlhmyddcbList = new ArrayList<>(); // 廉洁自律和满意度调查表
        ljzlhmyddcbList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.ljzlmyddcb)).collect(Collectors.toList());
        map.put("ljzlhmyddcbList",ljzlhmyddcbList);

        /*List<SpcySqsxWjjl> xcjcshjList = new ArrayList<>(); // 现场检查审核件
        xcjcshjList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.xcjcshj)).collect(Collectors.toList());
        map.put("xcjcshjList",xcjcshjList);*/

        String ywflCode = this.getYwflCode(sqsxid); // 业务分类编号
        if (StringUtils.isNotBlank(ywflCode) && ywflCode.equals(SpcyConstant.YWFL_CODE_HZP)){

            List<SpcySqsxWjjl> gcysmList = new ArrayList<>(); // 观察员声明
            gcysmList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                    && f.getFwjlx1().equals(WjmlConstant.gcysm)).collect(Collectors.toList());
            map.put("gcysmList",gcysmList);

            /*List<SpcySqsxWjjl> ljzlfkbList = new ArrayList<>(); // 廉洁自律反馈
            ljzlfkbList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.ljzlfk)).collect(Collectors.toList());
            map.put("ljzlfkbList",ljzlfkbList);*/

            List<SpcySqsxWjjl> xcjcblList = new ArrayList<>(); // 现场检查笔录
            xcjcblList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                    && f.getFwjlx1().equals(WjmlConstant.xcjcbl)).collect(Collectors.toList());
            map.put("xcjcblList",xcjcblList);

            List<SpcySqsxWjjl> xcjcjlHzpList = new ArrayList<>(); // 现场检查结论
            xcjcjlHzpList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                    && f.getFwjlx1().equals(WjmlConstant.hzp_xcjcjl)).collect(Collectors.toList());
            map.put("xcjcjlHzpList",xcjcjlHzpList);

        }else {
            List<SpcySqsxWjjl> jczgclyqList = new ArrayList<>(); // 检查整改材料要求
            jczgclyqList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                    && f.getFwjlx1().equals(WjmlConstant.jczgclyq)).collect(Collectors.toList());
            map.put("jczgclyqList",jczgclyqList);

            List<SpcySqsxWjjl> bgzsxhcbList = new ArrayList<>(); // 报告真实性核查表
            bgzsxhcbList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                    && f.getFwjlx1().equals(WjmlConstant.bgzsxhcb)).collect(Collectors.toList());
            map.put("bgzsxhcbList",bgzsxhcbList);

            List<SpcySqsxWjjl> xcjcbgList = new ArrayList<>(); // 现场检查报告
            xcjcbgList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                    && f.getFwjlx1().equals(WjmlConstant.xcjcbg)).collect(Collectors.toList());
            map.put("xcjcbgList",xcjcbgList);

            List<SpcySqsxWjjl> xcjcqxjlbList = new ArrayList<>(); // 现场检查缺陷记录表
            xcjcqxjlbList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                    && f.getFwjlx1().equals(WjmlConstant.xcjcqxjlb)).collect(Collectors.toList());
            map.put("xcjcqxjlbList",xcjcqxjlbList);

            if (StringUtils.isNotBlank(ywflCode) && ywflCode.equals(SpcyConstant.YWFL_CODE_YPJY)){

                List<SpcySqsxWjjl> xcjcbhgxmList = new ArrayList<>(); // 现场检查不合格项目
                xcjcbhgxmList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                        && f.getFwjlx1().equals(WjmlConstant.xcjcbhgxm)).collect(Collectors.toList());
                map.put("xcjcbhgxmList",xcjcbhgxmList);

                List<SpcySqsxWjjl> xcjcggList = new ArrayList<>(); // 现场检查公告
                xcjcggList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                        && f.getFwjlx1().equals(WjmlConstant.xcjcgg)).collect(Collectors.toList());
                map.put("xcjcggList",xcjcggList);

            }else if (StringUtils.isNotBlank(ywflCode) && (ywflCode.equals(SpcyConstant.YWFL_CODE_YPZC) || ywflCode.equals(SpcyConstant.YWFL_CODE_YPZCLC))){

                List<SpcySqsxWjjl> cydList = new ArrayList<>(); // 抽样单
                cydList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                        && f.getFwjlx1().equals(WjmlConstant.ypzc_cyd)).collect(Collectors.toList());
                map.put("cydList",cydList);

                List<SpcySqsxWjjl> cyfqList = new ArrayList<>(); // 抽样封签
                cyfqList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                        && f.getFwjlx1().equals(WjmlConstant.ypzc_cyfq)).collect(Collectors.toList());
                map.put("cyfqList",cyfqList);

            }

        }

        List<SpcySqsxWjjl> ljzlfkList = new ArrayList<>(); // 廉洁自律反馈表
        ljzlfkList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.ljzlfk)).collect(Collectors.toList());
        map.put("ljzlfkList",ljzlfkList);

        List<SpcySqsxWjjl> khmyddcList = new ArrayList<>(); // 客户满意度调查表
        khmyddcList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.khmyddc)).collect(Collectors.toList());
        map.put("khmyddcList",khmyddcList);

        return map;
    }

    /**
     * 获取初始化文件数据 - 查验 - 其他环节
     */
    public Map getInitQtWjjlList(String sqsxid){
        Map map = new HashMap();

        // 获取申请事项-相关文件(不属于行政审评的数据)
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid,sqsxid);
        List<SpcySqsxWjjl> wjjlList = spcySqsxWjjlService.list(queryWrapper);

        List<SpcySqsxWjjl> zhpdbgsList = new ArrayList<>(); // 综合评定报告书
        zhpdbgsList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.zhpdbgs)).collect(Collectors.toList());
        map.put("zhpdbgsList",zhpdbgsList);

        List<SpcySqsxWjjl> yshmbList = new ArrayList<>(); // 移送函模板
        yshmbList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.yshmb)).collect(Collectors.toList());
        map.put("yshmbList",yshmbList);

        String ywflCode = this.getYwflCode(sqsxid); // 业务分类编号

        if (StringUtils.isNotBlank(ywflCode) && ywflCode.equals(SpcyConstant.YWFL_CODE_HZP)){

            List<SpcySqsxWjjl> zgtzsList = new ArrayList<>(); // 整改通知书
            zgtzsList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                    && f.getFwjlx1().equals(WjmlConstant.zgtzs)).collect(Collectors.toList());
            map.put("zgtzsList",zgtzsList);

            List<SpcySqsxWjjl> zgqrbgsList = new ArrayList<>(); // 整改确认报告书
            zgqrbgsList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                    && f.getFwjlx1().equals(WjmlConstant.zgqrbgs)).collect(Collectors.toList());
            map.put("zgqrbgsList",zgqrbgsList);

        }else {

            if (StringUtils.isNotBlank(ywflCode) && ywflCode.equals(SpcyConstant.YWFL_CODE_YPJY)){
                List<SpcySqsxWjjl> zgtzsList = new ArrayList<>(); // 整改通知书
                zgtzsList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                        && f.getFwjlx1().equals(WjmlConstant.zgtzs)).collect(Collectors.toList());
                map.put("zgtzsList",zgtzsList);

                List<SpcySqsxWjjl> zgqrbgsList = new ArrayList<>(); // 整改确认报告书
                zgqrbgsList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                        && f.getFwjlx1().equals(WjmlConstant.zgqrbgs)).collect(Collectors.toList());
                map.put("zgqrbgsList",zgqrbgsList);
            }

            if (StringUtils.isNotBlank(ywflCode) && (ywflCode.equals(SpcyConstant.YWFL_CODE_YPZC) || ywflCode.equals(SpcyConstant.YWFL_CODE_YPZCLC))){
                List<SpcySqsxWjjl> zgtzsList = new ArrayList<>(); // 整改通知书
                zgtzsList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                        && f.getFwjlx1().equals(WjmlConstant.zgtzs)).collect(Collectors.toList());
                map.put("zgtzsList",zgtzsList);

                List<SpcySqsxWjjl> zgqrbgsList = new ArrayList<>(); // 整改确认报告书
                zgqrbgsList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                        && f.getFwjlx1().equals(WjmlConstant.zgqrbgs)).collect(Collectors.toList());
                map.put("zgqrbgsList",zgqrbgsList);
            }

            List<SpcySqsxWjjl> bztzList = new ArrayList<>(); // 补正通知
            bztzList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                    && f.getFwjlx1().equals(WjmlConstant.bztz)).collect(Collectors.toList());
            map.put("bztzList",bztzList);

            List<SpcySqsxWjjl> jczgclyqList = new ArrayList<>(); // 检查整改材料要求
            jczgclyqList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                    && f.getFwjlx1().equals(WjmlConstant.jczgclyq)).collect(Collectors.toList());
            map.put("jczgclyqList",jczgclyqList);

            List<SpcySqsxWjjl> xcjcqxjlbList = new ArrayList<>(); // 现场检查缺陷记录表
            xcjcqxjlbList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                    && f.getFwjlx1().equals(WjmlConstant.xcjcqxjlb)).collect(Collectors.toList());
            map.put("xcjcqxjlbList",xcjcqxjlbList);

        }

        return map;
    }

    /**
     * 获取补齐补正材料
     */
    public List<SpcySqsxWjjl> queryBqbzWjjlList(String sqsxid){
        List<SpcySqsxWjjl> wjjlList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                .eq(SpcySqsxWjjl::getFpid, sqsxid)
                .eq(SpcySqsxWjjl::getFwjlx1, WjmlConstant.bqbzcl));

        return wjjlList;
    }

    /**
     * 获取整改材料
     */
    public List<SpcySqsxWjjl> queryZgWjjlList(String sqsxid){
        List<SpcySqsxWjjl> wjjlList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                .eq(SpcySqsxWjjl::getFpid, sqsxid)
                .eq(SpcySqsxWjjl::getFwjlx1, WjmlConstant.zgcl));

        return wjjlList;
    }

    /**
     * 中心制作材料
     */
    public List<SpcySqsxWjjl> queryZxzzWjjlList(String sqsxid){
        List<SpcySqsxWjjl> wjjlList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                .eq(SpcySqsxWjjl::getFpid, sqsxid)
                .eq(SpcySqsxWjjl::getFwjlx1, WjmlConstant.wjml_type_2_zxzzcl));

        return wjjlList;
    }

    /**
     * 获取初始化文件数据 - 审评 - 方案制作环节
     * @param sqsxid 申请事项id
     */
    public Map getInitSpFazzWjjlList(String sqsxid){
        Map map = new HashMap();

        // 获取申请事项-相关文件(不属于行政审评的数据)
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid,sqsxid);
        List<SpcySqsxWjjl> wjjlList = spcySqsxWjjlService.list(queryWrapper);

        List<SpcySqsxWjjl> spfaList = new ArrayList<>(); // 审评方案
        spfaList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.spfa)).collect(Collectors.toList());
        map.put("spfaList",spfaList);

        List<SpcySqsxWjjl> hstzList = new ArrayList<>(); // 函审通知
        hstzList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.hstz)).collect(Collectors.toList());
        map.put("hstzList",hstzList);

        List<SpcySqsxWjjl> qytzsList = new ArrayList<>(); // 企业通知书
        qytzsList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.qytzs)).collect(Collectors.toList());
        map.put("qytzsList",qytzsList);

        List<SpcySqsxWjjl> zjspyjList = new ArrayList<>(); // 专家审评意见
        zjspyjList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.zjspyj)).collect(Collectors.toList());

        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser != null && (currentUser.getDeptName().equals(SpcyConstant.HCY_ZJ_ORGNAME) || currentUser.getDeptName().equals(SpcyConstant.HCY_ORGNAME))){
            String czrid = currentUser.getId();
            if (CollectionUtils.isNotEmpty(zjspyjList)) {
                zjspyjList = zjspyjList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx7())
                        && f.getFwjlx7().equals(czrid))
                        .collect(Collectors.toList());
            }
        }
        map.put("zjspyjList",zjspyjList);

        List<SpcySqsxWjjl> zxzhspyjList = new ArrayList<>(); // 中心综合审评意见
        zxzhspyjList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.ypzc_zxzhspyj)).collect(Collectors.toList());
        map.put("zxzhspyjList",zxzhspyjList);

        List<SpcySqsxWjjl> bztzList = new ArrayList<>(); // 补正通知
        bztzList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.bztz_YLqx)).collect(Collectors.toList());
        map.put("bztzList",bztzList);

        List<SpcySqsxWjjl> jsspbgList = new ArrayList<>(); // 技术审评报告
        jsspbgList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.jsspbg)).collect(Collectors.toList());
        map.put("jsspbgList",jsspbgList);

        List<SpcySqsxWjjl> qtclList = new ArrayList<>(); // 其他材料
        qtclList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.qtcl)).collect(Collectors.toList());
        map.put("qtclList",qtclList);

        return map;
    }

    /**
     * 获取初始化文件数据 - 审评 - 技术审评环节
     * @param sqsxid 申请事项id
     */
    public Map getInitJsspWjjlList(String sqsxid){
        Map map = new HashMap();

        // 获取申请事项-相关文件(不属于行政审评的数据)
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid,sqsxid);
        List<SpcySqsxWjjl> wjjlList = spcySqsxWjjlService.list(queryWrapper);

        List<SpcySqsxWjjl> spfaList = new ArrayList<>(); // 审评方案
        spfaList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.spfa)).collect(Collectors.toList());
        map.put("spfaList",spfaList);

        List<SpcySqsxWjjl> zjqdbList = new ArrayList<>(); // 专家签到表
        zjqdbList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.zjqdb)).collect(Collectors.toList());
        map.put("zjqdbList",zjqdbList);

        List<SpcySqsxWjjl> jsspbgList = new ArrayList<>(); // 技术审评报告
        jsspbgList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.jsspbg)).collect(Collectors.toList());
        map.put("jsspbgList",jsspbgList);

        List<SpcySqsxWjjl> jsspyjspbList = new ArrayList<>(); // 技术审评意见审批表
        jsspyjspbList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.jsspyjspb)).collect(Collectors.toList());
        map.put("jsspyjspbList",jsspyjspbList);

        List<SpcySqsxWjjl> zjspyjList = new ArrayList<>(); // 专家审评意见
        zjspyjList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.zjspyj)).collect(Collectors.toList());

        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser != null && (currentUser.getOrgName().equals(SpcyConstant.HCY_ZJ_ORGNAME) || currentUser.getOrgName().equals(SpcyConstant.HCY_ORGNAME))){
            String czrid = currentUser.getId();
            if (CollectionUtils.isNotEmpty(zjspyjList)) {
                zjspyjList = zjspyjList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx7())
                        && f.getFwjlx7().equals(czrid))
                        .collect(Collectors.toList());
            }
        }

        map.put("zjspyjList",zjspyjList);

        List<SpcySqsxWjjl> fhdwtzList = new ArrayList<>(); // 复核单位通知
        fhdwtzList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.ypzc_fhdwtz)).collect(Collectors.toList());
        map.put("fhdwtzList",fhdwtzList);

        List<SpcySqsxWjjl> zcctzList = new ArrayList<>(); // 注册处通知
        zcctzList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.ypzc_zcctz)).collect(Collectors.toList());
        map.put("zcctzList",zcctzList);

        return map;
    }

    /**
     * 获取申请事项-六大类文件记录（检查的情况专述、质量管理、机构与人员、厂房与设施、设备、物料与产品等）
     */
    public Map querySixTypeFileList(String sqsxid){
        Map result = new HashMap();

        // 获取申请事项-相关文件(不属于行政审评的数据)
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid,sqsxid);
        List<SpcySqsxWjjl> wjjlList = spcySqsxWjjlService.list(queryWrapper);

        List<SpcySqsxWjjl> jcdqkzsList = new ArrayList<>(); // 检查的情况专述
        jcdqkzsList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.SIX_TYPE_JCDQKZS)).collect(Collectors.toList());
        result.put("jcdqkzsList",jcdqkzsList);

        List<SpcySqsxWjjl> zlglList = new ArrayList<>(); // 质量管理
        zlglList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.SIX_TYPE_ZLGL)).collect(Collectors.toList());
        result.put("zlglList",zlglList);

        List<SpcySqsxWjjl> jgyryList = new ArrayList<>(); // 机构与人员
        jgyryList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.SIX_TYPE_JGYRY)).collect(Collectors.toList());
        result.put("jgyryList",jgyryList);

        List<SpcySqsxWjjl> cfyssList = new ArrayList<>(); // 厂房与设施
        cfyssList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.SIX_TYPE_CFYSS)).collect(Collectors.toList());
        result.put("cfyssList",cfyssList);

        List<SpcySqsxWjjl> sbList = new ArrayList<>(); // 设备
        sbList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.SIX_TYPE_SS)).collect(Collectors.toList());
        result.put("sbList",sbList);

        List<SpcySqsxWjjl> wlycpList = new ArrayList<>(); // 物料与产品
        wlycpList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.SIX_TYPE_WLYCP)).collect(Collectors.toList());
        result.put("wlycpList",wlycpList);

        List<SpcySqsxWjjl> qryyzList = new ArrayList<>(); // 确认与验证
        qryyzList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.SIX_TYPE_QRYYZ)).collect(Collectors.toList());
        result.put("qryyzList",qryyzList);

        List<SpcySqsxWjjl> wjqkList = new ArrayList<>(); // 文件情况
        wjqkList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.SIX_TYPE_WJQK)).collect(Collectors.toList());
        result.put("wjqkList",wjqkList);

        List<SpcySqsxWjjl> scglList = new ArrayList<>(); // 生产管理
        scglList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.SIX_TYPE_SCGL)).collect(Collectors.toList());
        result.put("scglList",scglList);

        List<SpcySqsxWjjl> zlkzyzlbzList = new ArrayList<>(); // 质量控制与质量保证
        zlkzyzlbzList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.SIX_TYPE_ZLKZYZLBZ)).collect(Collectors.toList());
        result.put("zlkzyzlbzList",zlkzyzlbzList);

        List<SpcySqsxWjjl> wtscywtjyList = new ArrayList<>(); // 委托生产与委托检验
        wtscywtjyList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.SIX_TYPE_WTSCYWTJY)).collect(Collectors.toList());
        result.put("wtscywtjyList",wtscywtjyList);

        List<SpcySqsxWjjl> cpfyyzhList = new ArrayList<>(); // 产品发运与召回
        cpfyyzhList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.SIX_TYPE_CPFYYZH)).collect(Collectors.toList());
        result.put("cpfyyzhList",cpfyyzhList);

        List<SpcySqsxWjjl> zjList = new ArrayList<>(); // 自检
        zjList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.SIX_TYPE_ZJ)).collect(Collectors.toList());
        result.put("zjList",zjList);


        return result;
    }

    /**
     * 获取申请事项-补齐补正历史
     */
    public List<SpcySqsxBqbzjl> getSqsxBqbzjlHistoryList(String sqsxid){
        List<SpcySqsxBqbzjl> list = spcySqsxBqbzjlService.list(new LambdaQueryWrapper<SpcySqsxBqbzjl>()
                .eq(SpcySqsxBqbzjl::getFsqsxid, sqsxid)
                .orderByAsc(SpcySqsxBqbzjl::getFfqrq));

        return list;
    }

    /**
     * 获取申请事项-补充整改历史
     */
    public List<SpcySqsxBczgjl> getSqsxBczgjlHistoryList(String sqsxid){
        List<SpcySqsxBczgjl> list = spcySqsxBczgjlService.list(new LambdaQueryWrapper<SpcySqsxBczgjl>()
                .eq(SpcySqsxBczgjl::getFsqsxid, sqsxid)
                .orderByAsc(SpcySqsxBczgjl::getFfqrq));

        return list;
    }

    /**
     * 根据主办人id-获取姓名
     */
    public String getZbrmcByZbrId(String zbrid){
        String mc = "";

        if (StringUtils.isBlank(zbrid)){
            return mc;
        }

        List<String> idLists = StringPatternUtils.getListByIds(zbrid);
        if (CollectionUtils.isEmpty(idLists)){
            return mc;
        }

        List<YcSysUser> userList = ycSysUserService.findUserJbxxListByIds(idLists);
        if (CollectionUtils.isEmpty(userList)){
            throw new BusinessException("请选择正确的办理人！");
//            return mc;
        }

        for (String userid : idLists){
            List<YcSysUser> collect = userList.stream()
                    .filter(f -> StringUtils.isNotBlank(f.getId())
                            && f.getId().equals(userid))
                    .collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(collect)){
                mc += StringUtils.isNotBlank(mc) ? "," : "";
                mc += collect.get(0).getRealname();
            }
        }

        return mc;
    }

    /**
     * 获取主办人或者科室负责人
     */
    public String getZbrHzKsfzr(SpcySqsxBasic sqsx){

        String dbrId = "";

        if (sqsx != null && StringUtils.isNotBlank(sqsx.getFzbr())){
            dbrId = sqsx.getFzbr();
        }

        if (StringUtils.isBlank(dbrId)){
            String ywflcode = "";
            if (sqsx != null && StringUtils.isNotBlank(sqsx.getFywflcode())){
                ywflcode = sqsx.getFywflcode();
            }

            Map ksfzrByDeptName = this.getKsfzrByDeptName(SpcyConstant.getYwclNameByCode(ywflcode));
            dbrId = ksfzrByDeptName.get("dbrId").toString();
        }

        return dbrId;
    }

    /**
     * 对应科室的 科室负责人
     * @param deptName
     * @return
     */
    public Map getKsfzrByDeptName(String deptName){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        List<GetKsKz> ksKzObject = new ArrayList<>();
        ksKzObject = ycSysUserService.getKsKzObject(deptName, SpcyConstant.GW_NAME_KZ);
        if (CollectionUtils.isNotEmpty(ksKzObject)) {
            for (GetKsKz ksKz : ksKzObject) {
                dbrId += ksKz.getId()+",";
                dbr += ksKz.getRealname()+",";
            }
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }

    /**
     * 通过申请事项id-判断该事项属于（注册/临床）
     * @return 0(注册) / 1(临床) / 2(未知)
     */
    public String verifyYpzcSqsxIsIfZc(SpcySqsxBasic sqsx){
        String flag = "2"; // 0(注册) / 1(临床) / 2(未知)

        String fsxmc = sqsx.getFsxmc(); // 事项名称

        if (StringUtils.isNotBlank(fsxmc)) {
            List<SpcyRsBhgz> list = spcyRsBhgzService.list(new LambdaQueryWrapper<SpcyRsBhgz>()
                    .eq(SpcyRsBhgz::getFywflcode, SpcyConstant.YWFL_CODE_YPZC)
                    .like(SpcyRsBhgz::getFtype2, fsxmc)
            );

            if (CollectionUtils.isNotEmpty(list)) {
                String ftype3 = list.get(0).getFtype3();
                if (StringUtils.isNotBlank(ftype3) && ftype3.contains(SpcyConstant.YPZC_ZC)){
                    flag = "0";
                }else if (StringUtils.isNotBlank(ftype3) && ftype3.contains(SpcyConstant.YPZC_LC)){
                    flag = "1";
                }
            }
        }

        return flag;
    }

    /** -------------------------------------------【 申请事项信息 - 功能 】--------------------------------------------------------- */

    /**
     * 保存申请事项-审查模式
     */
    public Result saveSqsxScms(String sqsxid,String fscms){
        if (StringUtils.isBlank(sqsxid)){
            return Result.error("申请事项id为空");
        }
        if (StringUtils.isBlank(fscms)){
            return Result.error("审查模式为空");
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return Result.error("当前申请事项为空，请重试");
        }

        if (StringUtils.isNotBlank(fscms)){
            if (fscms.equals(SpcyConstant.SQSX_SCMS_SMSCYJ)){
                fscms = SpcyConstant.SQSX_SCMS_JSSC;
            }else if (fscms.equals(SpcyConstant.SQSX_SCMS_SCJCANBSMSCYJ)){
                fscms = SpcyConstant.SQSX_SCMS_SCJCANBJSSC;
            }
        }

        LambdaUpdateWrapper<SpcySqsxBasic> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxBasic::getId, sqsxid);

        SpcySqsxBasic updateBean = new SpcySqsxBasic();
        updateBean.setFscms(fscms);

        spcySqsxBaiscService.update(updateBean, updateWrapper);

        return Result.ok("保存成功");
    }

    /**
     * 保存申请事项-审评类型
     */
    public Result saveSqsxJsspType(String sqsxid,String fsplx){
        if (StringUtils.isBlank(sqsxid)){
            return Result.error("申请事项id为空");
        }
        if (StringUtils.isBlank(fsplx)){
            return Result.error("审评类型为空");
        }

        LambdaUpdateWrapper<SpcySqsxBasic> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxBasic::getId, sqsxid);

        SpcySqsxBasic updateBean = new SpcySqsxBasic();
        updateBean.setFsplx(fsplx);
        updateBean.setFsfbcsplx("是");

        spcySqsxBaiscService.update(updateBean, updateWrapper);

        return Result.ok("保存成功");
    }

    /**
     * 修改申请事项表
     */
    public Result updateSqsxBasic(HashMap<String,Object> map,String sqsxid,SpcySqsxJob job){
        if (map == null || map.isEmpty()){
            return Result.ok();
        }
        SpcySqsxBasic oldBasic = spcySqsxBaiscService.getById(sqsxid);
        if (oldBasic == null){
            return Result.error("当前申请事项【"+sqsxid+"】不存在，请重试");
        }
        SpcySqsxBasic basicUpdate = oldBasic;

        String oldFslzt = basicUpdate.getFslzt(); // 受理状态

        // 修改
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(basicUpdate,map);
            if (StringUtils.isNotBlank(basicUpdate.getFywflcode())){
                // 获取业务分类名称
                String ywclNameByCode = SpcyConstant.getYwclNameByCode(oldBasic.getFywflcode());
                basicUpdate.setFywflname(ywclNameByCode);
            }

            // 验证报告编号是否存在
            if (StringUtils.isNotBlank(basicUpdate.getFslbh())) {
                Result result = IsExistsBgbh(sqsxid, basicUpdate.getFslbh());
                if (!result.isSuccess()) {
                    return Result.error(result.getMessage());
                }
            }

            // 验证评定编号是否存在
            if (StringUtils.isNotBlank(basicUpdate.getFpdbh())) {
                Result result = IsExistsPdbh(sqsxid, basicUpdate.getFpdbh());
                if (!result.isSuccess()) {
                    return Result.error(result.getMessage());
                }
            }

            // 验证移送函编号是否存在
            if (StringUtils.isNotBlank(basicUpdate.getFyshbh())) {
                Result result = IsExistsYshbh(sqsxid, basicUpdate.getFyshbh());
                if (!result.isSuccess()) {
                    return Result.error(result.getMessage());
                }
            }

            // 受理信息
            if (StringUtils.isNotBlank(oldFslzt) && oldFslzt.equals(basicUpdate.getFslzt())){
                basicUpdate.setFslr(GetCurrentUserUtils.getCurrentUser().getName());
            }

            boolean isExistWcsx = false; // 是否存在完成时限
            if (basicUpdate.getFwcsx() == null){
                isExistWcsx = true;
            } else {
                if (basicUpdate.getFwcsx() == 0){
                    isExistWcsx = true;
                }

                if (StringUtils.isBlank(basicUpdate.getFsysx())) {
                    basicUpdate.setFsysx(String.valueOf(basicUpdate.getFwcsx()));
                }
            }

            /** 更新检查时间 */
            if (oldBasic.getFcykssj() != null && basicUpdate.getFcykssj() != null){
                if (!oldBasic.getFcykssj().equals(basicUpdate.getFcykssj())){
                    spcySqsxXpjlService.syncUpdateXpjlGzsjBySqsx(sqsxid, basicUpdate.getFcykssj(), null);
                }
            }
            if (oldBasic.getFcyjssj() != null && basicUpdate.getFcyjssj() != null){
                if (!oldBasic.getFcyjssj().equals(basicUpdate.getFcyjssj())){
                    spcySqsxXpjlService.syncUpdateXpjlGzsjBySqsx(sqsxid, null, basicUpdate.getFcyjssj());
                }
            }

            spcySqsxBaiscService.updateById(basicUpdate);

            if (isExistWcsx){
                flowJobBlsxService.initSqsxBlsxBySqsxId(sqsxid);
            }

            /*if (basicUpdate.getFjzrq() == null) {
                Result result = flowJobBlsxService.jsSqsxJzrq(sqsxid);// 计算截止日期
                if (!result.isSuccess()){
                    return Result.error(result.getMessage());
                }
            }*/

            if (basicUpdate.getFwcsx() != null){
                if (oldBasic.getFwcsx() == null || oldBasic.getFwcsx() != basicUpdate.getFwcsx()){
                    Result result = flowJobBlsxService.jsSqsxJzrq(sqsxid);// 计算截止日期
                    if (!result.isSuccess()){
                        return Result.error(result.getMessage());
                    }
                }
            }

            // 修改企业
            if (StringUtils.isNotBlank(basicUpdate.getFtyshxydm())
                    && StringUtils.isBlank(basicUpdate.getFqyid())){
                List<SpcyQyJbxx> qyjbxx = spcyQyJbxxService.list(new LambdaQueryWrapper<SpcyQyJbxx>()
                        .eq(SpcyQyJbxx::getFtyshxydm, basicUpdate.getFtyshxydm()));

                if (CollectionUtils.isEmpty(qyjbxx)){
                    SpcyQyJbxx qy = new SpcyQyJbxx();
                    qy.setId(GetUuIdUtils.ReplaceUuId());
                    BeanUtils.populate(qy,map);
                    spcyQyJbxxService.save(qy);

                    // 事项绑定企业
                    if (StringUtils.isBlank(basicUpdate.getFqyid())) {
                        basicUpdate.setFqyid(qy.getId());
                        spcySqsxBaiscService.updateById(basicUpdate);
                    }
                }else {
                    SpcyQyJbxx qyJbxx = qyjbxx.get(0);
                    BeanUtils.populate(qyJbxx,map);
                    spcyQyJbxxService.updateById(qyJbxx);

                    // 事项绑定企业
                    if (StringUtils.isBlank(basicUpdate.getFqyid())) {
                        basicUpdate.setFqyid(qyJbxx.getId());
                        spcySqsxBaiscService.updateById(basicUpdate);
                    }

                }
            }else if (StringUtils.isNotBlank(basicUpdate.getFqyid())){
                SpcyQyJbxx qyJbxx = spcyQyJbxxService.getById(basicUpdate.getFqyid());
                if (qyJbxx != null){
                    BeanUtils.populate(qyJbxx, map);
                    spcyQyJbxxService.updateById(qyJbxx);
                }
            }

            // 日常监管
            spcySqsxRcjgService.syncUpdateRcjg(sqsxid, basicUpdate.getFxzspzt(), basicUpdate.getFjclx(), map);

        } catch (IllegalAccessException e) {
            log.error("出错-保存申请事项【非法访问异常】,{}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("出错-保存申请事项【调用TargetException】,{}",e.getMessage());
        }

        String logContent = "基本信息修改: ";
        Map<String, Object> basicMap = ObjectToMapUtils.objectToMap(oldBasic);
        logContent += LogUtils.BeanModifyMsg(basicUpdate,basicMap);

        if (job == null) {
            spcyLogService.addLogs(LogConstant.LOGS_SQSX_BASIC, logContent, sqsxid, LogConstant.LOGS_SQSX_BASIC, SpcyConstant.LOGS_SYS_TYPE_XG);
        }else {
            spcyLogService.addLogs(job.getFhjmc(), logContent, sqsxid, LogConstant.LOGS_SQSX_BASIC, SpcyConstant.LOGS_SYS_TYPE_XG);
        }

        return Result.ok("保存成功");
    }

    /**
     * 修改申请事项表 - 基本信息
     */
    public Result updateSqsxJbxx(HashMap<String,Object> map,String sqsxid,SpcySqsxJob job){
        SpcySqsxBasic oldBasic = spcySqsxBaiscService.getById(sqsxid);
        if (oldBasic == null){
            return Result.error("当前申请事项【"+sqsxid+"】不存在，请重试");
        }
        SpcySqsxBasic basicUpdate = null;
        basicUpdate = oldBasic;

        // 修改
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(basicUpdate,map);
            spcySqsxBaiscService.updateById(basicUpdate);
        } catch (IllegalAccessException e) {
            log.error("出错-保存申请事项【非法访问异常】,{}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("出错-保存申请事项【调用TargetException】,{}",e.getMessage());
        }

        String logContent = "基本信息修改: ";
        Map<String, Object> basicMap = ObjectToMapUtils.objectToMap(oldBasic);
        logContent += LogUtils.BeanModifyMsg(basicUpdate,basicMap);

        if (job == null) {
            spcyLogService.addLogs(LogConstant.LOGS_SQSX_BASIC, logContent, sqsxid, LogConstant.LOGS_SQSX_BASIC, SpcyConstant.LOGS_SYS_TYPE_XG);
        }else {
            spcyLogService.addLogs(job.getFhjmc(), logContent, sqsxid, LogConstant.LOGS_SQSX_BASIC, SpcyConstant.LOGS_SYS_TYPE_XG);
        }

        return Result.ok("保存成功");
    }

    /**
     * 判断报告编号是否存在
     * @param sqsxId 申请事项id
     * @param bgbh 报告编号
     */
    public Result IsExistsBgbh(String sqsxId,String bgbh){
        // 查当前这条数据的
        SpcySqsxBasic sqsxBaisc = spcySqsxBaiscService.selectSqsxInfoById(sqsxId);
       /* if (sqsxBaisc == null){
            return "";
        }*/

        // 查页面传过来的编号查询
        List<SpcySqsxBasic> bgbhList = new ArrayList<>();
        bgbhList = spcySqsxBaiscService.list(new LambdaQueryWrapper<SpcySqsxBasic>()
                .eq(SpcySqsxBasic::getFslbh, bgbh)
                .isNotNull(SpcySqsxBasic::getFslbh)
                .eq(StringUtils.isNotBlank(sqsxBaisc.getFywflcode()),SpcySqsxBasic::getFywflcode,sqsxBaisc.getFywflcode()));

        // 报告编号存在
        if (CollectionUtils.isNotEmpty(bgbhList)) {
            for (SpcySqsxBasic report : bgbhList){
                // 判断报告编号 主键相同
                if (report.getId().equals(sqsxBaisc.getId())){

                }else {
                    // 报告编号 主键不相同
                    if (StringUtils.isEmpty(sqsxBaisc.getFslbh())) {
                        // 报告编号列表存在，当前数据的报告编号也为空，所有评定存在
                        return Result.error("受理编号已存在，请重新输入");
                    } else {
                        // 当前数据的报告编号不为空
                        if (report.getFslbh().equals(bgbh)) { // 当前数据报告编号和 报告编号列表中的编号 相同，评定存在
                            return Result.error("受理编号已存在，请重新输入");
                        } else {

                        }
                    }
                }
            }
        }else {
            return Result.ok("");
        }

        return Result.ok("");
    }

    /**
     * 判断评定编号是否存在
     * @param sqsxId 申请事项id
     * @param pdbh 评定编号
     */
    public Result IsExistsPdbh(String sqsxId,String pdbh){
        // 查当前这条数据的
        SpcySqsxBasic sqsxBaisc = spcySqsxBaiscService.getById(sqsxId);
       /* if (sqsxBaisc == null){
            return "";
        }*/

        // 查页面传过来的编号查询
        List<SpcySqsxBasic> bgbhList = new ArrayList<>();
        bgbhList = spcySqsxBaiscService.list(new LambdaQueryWrapper<SpcySqsxBasic>()
                .eq(SpcySqsxBasic::getFpdbh, pdbh)
                .isNotNull(SpcySqsxBasic::getFpdbh)
                .eq(StringUtils.isNotBlank(sqsxBaisc.getFywflcode()),SpcySqsxBasic::getFywflcode,sqsxBaisc.getFywflcode()));

        // 报告评定存在
        if (CollectionUtils.isNotEmpty(bgbhList)) {
            for (SpcySqsxBasic report : bgbhList){
                // 判断评定编号 主键相同
                if (report.getId().equals(sqsxBaisc.getId())){

                }else {
                    // 评定编号 主键不相同
                    if (StringUtils.isEmpty(sqsxBaisc.getFpdbh())) {
                        // 评定编号列表存在，当前数据的报告编号也为空，所有评定存在
                        return Result.error("评定编号已存在，请重新输入");
                    } else {
                        // 当前数据的报告编号不为空
                        if (report.getFpdbh().equals(pdbh)) { // 当前数据报告编号和 报告编号列表中的编号 相同，评定存在
                            return Result.error("评定编号已存在，请重新输入");
                        } else {

                        }
                    }
                }
            }
        }else {
            return Result.ok("");
        }

        return Result.ok("");
    }


    /**
     * 判断移送函编号是否存在
     * @param sqsxId 申请事项id
     * @param yshbh 移送函编号
     */
    public Result IsExistsYshbh(String sqsxId,String yshbh){

        // 查当前这条数据的
        SpcySqsxBasic sqsxBaisc = spcySqsxBaiscService.selectSqsxInfoById(sqsxId);
        if (sqsxBaisc == null){
            throw new BusinessException("当前申请事项不存在，请联系管理员！");
        }

        // 验证移送函编号规范
        this.isFhgfYshbh(yshbh,sqsxBaisc.getFywflcode());

        // 查页面传过来的编号查询
        List<SpcySqsxBasic> bgbhList = new ArrayList<>();
        bgbhList = spcySqsxBaiscService.list(new LambdaQueryWrapper<SpcySqsxBasic>()
                .eq(SpcySqsxBasic::getFyshbh, yshbh)
                .isNotNull(SpcySqsxBasic::getFyshbh)
                .eq(StringUtils.isNotBlank(sqsxBaisc.getFywflcode()),SpcySqsxBasic::getFywflcode,sqsxBaisc.getFywflcode()));

        // 移送函编号存在
        if (CollectionUtils.isNotEmpty(bgbhList)) {
            for (SpcySqsxBasic report : bgbhList){
                // 判断移送函编号 主键相同
                if (report.getId().equals(sqsxBaisc.getId())){

                }else {
                    // 移送函编号 主键不相同
                    if (StringUtils.isEmpty(sqsxBaisc.getFyshbh())) {
                        // 移送函编号列表存在，当前数据的报告编号也为空，所有评定存在
                        return Result.error("移送函编号已存在，请重新输入");
                    } else {
                        // 当前数据的移送函编号不为空
                        if (report.getFyshbh().equals(yshbh)) { // 当前数据报告编号和 报告编号列表中的编号 相同，评定存在
                            return Result.error("移送函编号已存在，请重新输入");
                        } else {

                        }
                    }
                }
            }
        }else {
            return Result.ok("");
        }

        return Result.ok("");
    }

    /**
     * 判断移送函编号-是否符合规范
     */
    public void isFhgfYshbh(String yshbh, String fywflcode){
        if (StringUtils.isBlank(yshbh) || StringUtils.isBlank(fywflcode)){
            return;
        }

        String baseYwfl = fywflcode;
        baseYwfl = this.replaceYwflcode(baseYwfl);

        List<SpcyRsBhgz> bghzTypeList = spcyRsBhgzService.list(new LambdaQueryWrapper<SpcyRsBhgz>()
                .eq(SpcyRsBhgz::getFywflcode, baseYwfl)
                .eq(SpcyRsBhgz::getFtype1, BhgzConstant.YSHBH));

        if (CollectionUtils.isNotEmpty(bghzTypeList)){
            String bhgz = bghzTypeList.get(0).getFqz();

            if (StringUtils.isBlank(bhgz)){
                return;
            }

            // 是否符合规范
            boolean isBfhgf = false;
            if (yshbh.contains(bhgz)
                    && yshbh.contains(Character.toString(BhgzConstant.zkh))
                    && yshbh.contains(Character.toString(BhgzConstant.ykh))
                    && yshbh.contains(Character.toString(BhgzConstant.jwzf))){

                // 号 后面不能跟字符
                String hzStr = yshbh.substring(yshbh.indexOf(Character.toString(BhgzConstant.jwzf)));
                if (StringUtils.isNotBlank(hzStr) && !hzStr.equals(Character.toString(BhgzConstant.jwzf))){
                    isBfhgf = true;
                }

                if (!isBfhgf) {
                    int qzLength = bhgz.length(); // 前缀长度
                    String qzString = yshbh.substring(0, qzLength);
                    if (StringUtils.isBlank(qzString) || !qzString.equals(bhgz)) {
                        isBfhgf = true;
                    }
                }

                // 左括号数量
                if (!isBfhgf) {
                    String[] zkhSplit = yshbh.split(Character.toString(BhgzConstant.zkh));
                    int zkhcd = zkhSplit.length;
                    if (zkhcd > 2) {
                        isBfhgf = true;
                    }
                }

                // 右括号数量
                if (!isBfhgf) {
                    String[] ykhSplit = yshbh.split(Character.toString(BhgzConstant.ykh));
                    int ykhcd = ykhSplit.length;
                    if (ykhcd > 2) {
                        isBfhgf = true;
                    }
                }

                // 结尾后缀数量
                if (!isBfhgf) {
                    String[] hzSplit = yshbh.split(Character.toString(BhgzConstant.jwzf));
                    int hzcd = hzSplit.length;
                    if (hzcd > 2) {
                        isBfhgf = true;
                    }
                }

                if (!isBfhgf){
                    // 截取两个字符中间的值
                    String midStr = StringUtils.substringBetween(yshbh, Character.toString(BhgzConstant.ykh), Character.toString(BhgzConstant.jwzf));
                    if (StringUtils.isBlank(midStr) || !StringPatternUtils.verifyCsz(midStr)){
                        isBfhgf = true;
                    }
                }

            }else {
                isBfhgf = true;
            }

            if (isBfhgf) {
                String nowYear = DateConversionUtils.DateToYear(new Date()); // 当前年份
                String bhgzmb = bhgz + BhgzConstant.zkh + nowYear + BhgzConstant.ykh + "1" + BhgzConstant.jwzf;
                throw new BusinessException("当前移送函编码不符合规范 [ " + yshbh + " ]，模板如下：[ " + bhgzmb + " ] ");
            }
        }
    }


    /**
     * 各科室-根据事项名称获取对应的业务类型（检查类型）
     */
    public String getDeptJclxBySxmc(String sxmc,String fywflcode){
        if (StringUtils.isBlank(sxmc)){
            return "";
        }

        if (StringUtils.isBlank(fywflcode)){
            return "";
        }

        fywflcode = this.replaceYwflcode(fywflcode);

        // 药品注册 事项名称-对应-检查类型
        /*if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPZC)) {
            List<SpcyRsBhgz> bhgzList = spcyRsBhgzService.list(new LambdaQueryWrapper<SpcyRsBhgz>()
                    .eq(SpcyRsBhgz::getFywflcode, fywflcode)
                    .isNotNull(SpcyRsBhgz::getFtype2)
                    .like(SpcyRsBhgz::getFtype2, sxmc));

            return bhgzList.get(0).getFtype1();

        }else {*/

        List<SpcyRsBhgz> bhgzList = spcyRsBhgzService.list(new LambdaQueryWrapper<SpcyRsBhgz>()
                .eq(SpcyRsBhgz::getFywflcode, fywflcode)
                .like(SpcyRsBhgz::getFtype2, sxmc));

        if (CollectionUtil.isNotEmpty(bhgzList)) {
            return bhgzList.get(0).getFtype1();
        }else {
            return "";
        }

    }

    /**
     * 保存-申请事项-检查项目
     */
    public Result updateSqsxJcxm(HashMap<String, String> buffer){
        if (StringUtils.isBlank(buffer.get("id"))){
            return Result.error("检查项目id不能为空");
        }
        String id = buffer.get("id");
        SpcySqsxJcxm nowJcxmUpdate = spcySqsxJcxmService.getById(id);
        if (nowJcxmUpdate == null){
            return Result.error("【"+id+"】检查项目数据不存在，请重试");
        }
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(nowJcxmUpdate);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        try {
            BeanUtils.populate(nowJcxmUpdate, buffer);
        } catch (IllegalAccessException e) {
            log.error("修改申请检查项目出错-【IllegalAccessException】- {}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("修改申请检查项目出错-【InvocationTargetException】",e.getMessage());
        }
        spcySqsxJcxmService.updateById(nowJcxmUpdate);

        String nowNodeName = getNowNodeName(nowJcxmUpdate.getFsqsxid()); // 当前环节名称
        String operContent = "申请事项-检查项目-基本信息修改：";
        operContent += LogUtils.BeanModifyMsg(nowJcxmUpdate,oldMap);
        spcyLogService.addLogs(nowNodeName,operContent,nowJcxmUpdate.getFsqsxid(),LogConstant.LOGS_SQSX_JCXM,SpcyConstant.LOGS_SYS_TYPE_XG);

        return Result.ok("");
    }

    /**
     * 保存-药品流通-检查项目
     */
    public Result updateYpltJcxm(HashMap<String,String> jcxmVo){
        if (StringUtils.isBlank(jcxmVo.get("id"))){
            return Result.error("检查项目id不能为空");
        }
        String id = jcxmVo.get("id");
        SpcySqsxJcxm nowJcxmUpdate = spcySqsxJcxmService.getById(id);
        if (nowJcxmUpdate == null){
            return Result.error("【"+id+"】检查项目数据不存在，请重试");
        }
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(nowJcxmUpdate);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        try {
            // BeanUtils.describe(
            BeanUtils.populate(nowJcxmUpdate, jcxmVo);
        } catch (IllegalAccessException e) {
            log.error("修改申请检查项目出错-【IllegalAccessException】- {}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("修改申请检查项目出错-【InvocationTargetException】- {}", e.getMessage());
        }
//        }catch (NoSuchMethodException e) {
//            log.error("修改申请检查项目出错-【NoSuchMethodException】- {}",e.getMessage());
//        }
        /** 药品流通： ftype1 科室, ftype2 类型,ftype3 章节,ftype4 章节标题,
         *   ftype5 条款, ftype6 (一般项目 |一般重点项目 | 重点项目) 项目类型，ftype7 条款号, fcontent1 审查内容
         */
        // 审查记录内容不为空 ， 自动对应 缺陷（一般缺陷、主要缺陷、严重缺陷）
        if (StringUtils.isNotBlank(nowJcxmUpdate.getFscjl())){
            if (StringUtils.isNotBlank(nowJcxmUpdate.getFtype6())){
                // 两个** 为严重, 一个* 为主要，无* 为一般
                if (nowJcxmUpdate.getFtype6().equals(SpcyConstant.YPLT_YBXM)){
                    nowJcxmUpdate.setFjcjl(SpcyConstant.REVIEW_RECORD_CONCLUSION_YBQX);

                }else if (nowJcxmUpdate.getFtype6().equals(SpcyConstant.YPLT_YBZDXM)){
                    nowJcxmUpdate.setFjcjl(SpcyConstant.REVIEW_RECORD_CONCLUSION_ZYQX);

                }else if (nowJcxmUpdate.getFtype6().equals(SpcyConstant.YPLT_ZDXM)){
                    nowJcxmUpdate.setFjcjl(SpcyConstant.REVIEW_RECORD_CONCLUSION_YZQX);
                }
            }else {
                nowJcxmUpdate.setFjcjl(SpcyConstant.REVIEW_RECORD_CONCLUSION_YBQX);
            }
        }else {
            nowJcxmUpdate.setFscjl("");
            nowJcxmUpdate.setFjcjl(SpcyConstant.REVIEW_RECORD_CONCLUSION_WQX);
        }
        /*if (StringUtils.isBlank(nowJcxmUpdate.getFscjl())){
            nowJcxmUpdate.setFjcjl(SpcyConstant.REVIEW_RECORD_CONCLUSION_WQX);
        }*/
        spcySqsxJcxmService.updateById(nowJcxmUpdate);

        String nowNodeName = getNowNodeName(nowJcxmUpdate.getFsqsxid()); // 当前环节名称
        String operContent = "申请事项-检查项目-基本信息修改：";
        operContent += LogUtils.BeanModifyMsg(nowJcxmUpdate,oldMap);
        spcyLogService.addLogs(nowNodeName,operContent,nowJcxmUpdate.getFsqsxid(),LogConstant.LOGS_SQSX_JCXM,SpcyConstant.LOGS_SYS_TYPE_XG);

        return Result.ok("");
    }

    /**
     *  保存-药品注册-检查项目
     */
    public Result updateYpzcJcxm(HashMap<String, String> jcxmVo){
        if (StringUtils.isBlank(jcxmVo.get("id"))){
            return Result.error("检查项目id不能为空");
        }
        String id = jcxmVo.get("id");
        SpcySqsxJcxm nowJcxmUpdate = spcySqsxJcxmService.getById(id);
        if (nowJcxmUpdate == null){
            return Result.error("【"+id+"】检查项目数据不存在，请重试");
        }
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(nowJcxmUpdate);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        try {
            // BeanUtils.describe(
            BeanUtils.populate(nowJcxmUpdate, jcxmVo);
        } catch (IllegalAccessException e) {
            log.error("修改申请检查项目出错-【IllegalAccessException】- {}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("修改申请检查项目出错-【InvocationTargetException】- {}", e.getMessage());
        }

        /** 药品注册： ftype1 科室, ftype2 检查类型,ftype3 大类,ftype4 类型,
         *   ftype5 序号, ftype6 (一般项目 |一般重点项目 | 重点项目) 项目类型，ftype7 条款号, fcontent1 检查项目,fcontent2 检查要点
         */
        // 审查记录内容不为空 ， 自动对应 缺陷（一般缺陷、主要缺陷、严重缺陷）
        if (StringUtils.isNotBlank(nowJcxmUpdate.getFscjl())){
            if (StringUtils.isNotBlank(nowJcxmUpdate.getFtype6())){
                // 两个** 为严重, 一个* 为主要，无* 为一般
                if (nowJcxmUpdate.getFtype6().equals(SpcyConstant.YPZC_YBXM)){
                    nowJcxmUpdate.setFjcjl(SpcyConstant.REVIEW_RECORD_CONCLUSION_BFH);

                }else if (nowJcxmUpdate.getFtype6().equals(SpcyConstant.YPZC_ZDXM)){
                    nowJcxmUpdate.setFjcjl(SpcyConstant.REVIEW_RECORD_CONCLUSION_BFH);
                }
            }else {
                nowJcxmUpdate.setFjcjl(SpcyConstant.REVIEW_RECORD_CONCLUSION_BFH);
            }
        }else {
            nowJcxmUpdate.setFscjl("");
            nowJcxmUpdate.setFjcjl(SpcyConstant.REVIEW_RECORD_CONCLUSION_FH);
        }

        spcySqsxJcxmService.updateById(nowJcxmUpdate);

        String nowNodeName = getNowNodeName(nowJcxmUpdate.getFsqsxid()); // 当前环节名称
        String operContent = "申请事项-检查项目-基本信息修改：";
        operContent += LogUtils.BeanModifyMsg(nowJcxmUpdate,oldMap);
        spcyLogService.addLogs(nowNodeName,operContent,nowJcxmUpdate.getFsqsxid(),LogConstant.LOGS_SQSX_JCXM,SpcyConstant.LOGS_SYS_TYPE_XG);

        return Result.ok("");
    }

    /**
     *  保存-化妆品-检查项目
     */
    public Result updateHzpJcxm(HashMap<String, String> jcxmVo){
        if (StringUtils.isBlank(jcxmVo.get("id"))){
            return Result.error("检查项目id不能为空");
        }
        String id = jcxmVo.get("id");
        SpcySqsxJcxm nowJcxmUpdate = spcySqsxJcxmService.getById(id);
        if (nowJcxmUpdate == null){
            return Result.error("【"+id+"】检查项目数据不存在，请重试");
        }
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(nowJcxmUpdate);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        try {
            // BeanUtils.describe(
            BeanUtils.populate(nowJcxmUpdate, jcxmVo);
        } catch (IllegalAccessException e) {
            log.error("修改申请检查项目出错-【IllegalAccessException】- {}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("修改申请检查项目出错-【InvocationTargetException】- {}", e.getMessage());
        }

        /** 药品注册： ftype1 科室, ftype2 检查类型,ftype3 大类,ftype4 类型,
         *   ftype5 序号, ftype6 (一般项目 |一般重点项目 | 重点项目) 项目类型，ftype7 条款号, fcontent1 检查项目,fcontent2 检查要点
         */
        // 审查记录内容不为空 ， 自动对应 缺陷（一般缺陷、主要缺陷、严重缺陷）
        if (StringUtils.isNotBlank(nowJcxmUpdate.getFscjl())){
            if (StringUtils.isNotBlank(nowJcxmUpdate.getFtype7())){
                // 两个** 为关键项目, 一个* 为重点项目，无* 为一般
                if (nowJcxmUpdate.getFtype7().contains("*")){

                }else if (nowJcxmUpdate.getFtype7().contains("**")){

                }else {
                    nowJcxmUpdate.setFjcjl(SpcyConstant.REVIEW_RECORD_CONCLUSION_BFHGD);
                }
            }else {
                nowJcxmUpdate.setFjcjl(SpcyConstant.REVIEW_RECORD_CONCLUSION_BFHGD);
            }
        }else {
            nowJcxmUpdate.setFscjl("");
            nowJcxmUpdate.setFjcjl(SpcyConstant.REVIEW_RECORD_CONCLUSION_FHGD);
        }

        spcySqsxJcxmService.updateById(nowJcxmUpdate);

        String nowNodeName = getNowNodeName(nowJcxmUpdate.getFsqsxid()); // 当前环节名称
        String operContent = "申请事项-检查项目-基本信息修改：";
        operContent += LogUtils.BeanModifyMsg(nowJcxmUpdate,oldMap);
        spcyLogService.addLogs(nowNodeName,operContent,nowJcxmUpdate.getFsqsxid(),LogConstant.LOGS_SQSX_JCXM,SpcyConstant.LOGS_SYS_TYPE_XG);

        return Result.ok("");
    }

    /** -------------------------------------------【 获取流程任务相关信息 - 功能 】--------------------------------------------------------- */

    public SpcyQyJbxx getQyjbxx(String qyid){
        // 企业相关信息
        SpcyQyJbxx qyJbxx = spcyQyJbxxService.getById(qyid);

        return qyJbxx;
    }

    /**
     * 查询申请事项-详情信息-通过申请事项id
     */
    public Map querySqsxDetailsById(String sqsxid){
        Map map = new HashMap();

        // 申请事项基本信息
        SpcySqsxBasic sqsxBasic = spcySqsxBaiscService.getById(sqsxid);

        // 流程任务流传记录
        List<SpcySqsxJob> jobList = getJobListBySqsxId(sqsxid);

        map.put("sqsx",sqsxBasic); // 申请事项基本信息
        map.put("jobHistoryList",jobList); // 流程任务流传记录

        // 企业相关信息
        SpcyQyJbxx qyJbxx = null;
        if (sqsxBasic != null && StringUtils.isNotBlank(sqsxBasic.getFqyid())){
            qyJbxx = spcyQyJbxxService.getById(sqsxBasic.getFqyid());
        }
        map.put("qy",qyJbxx);

        String fywflcode = sqsxBasic.getFywflcode();

        if (StringUtils.isNotBlank(fywflcode)) {
            // 补正流程
            if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPSC) || fywflcode.equals(SpcyConstant.YWFL_CODE_HZP)
                    || fywflcode.equals(SpcyConstant.YWFL_CODE_YPZC) || fywflcode.equals(SpcyConstant.YWFL_CODE_YPZCLC)) {
                map.put("bzlcJobList", this.getSubFlowJobList(sqsxid, SpBzFlowConstant.FLOW_NAME));
            }

            // 专家流程
            if (fywflcode.equals(SpcyConstant.YWFL_CODE_YLQX) || fywflcode.equals(SpcyConstant.YWFL_CODE_YPZC)
                    || fywflcode.equals(SpcyConstant.YWFL_CODE_YPZCLC)) {
                map.put("zjlcJobList", this.getSubFlowJobList(sqsxid, SpcyZjFlowConstant.FLOW_NAME));
            }
        }

        // 整改确认流程
        map.put("zgqrlcJobList", this.getSubFlowJobList(sqsxid, SpZgqrFlowConstant.FLOW_NAME));

        // 技术审查报告流程
        if (StringUtils.isNotBlank(fywflcode) && fywflcode.equals(SpcyConstant.YWFL_CODE_YPJY)) {
            map.put("jsscbglcJobList", this.getSubFlowJobList(sqsxid, JsscbgFlowConstant.FLOW_NAME));
        }

        return map;
    }

    /**
     * 获取子流程-流程记录
     */
    public List<SpcySqsxJob> getSubFlowJobList(String sqsxid,String lcmc){
        LambdaQueryWrapper<SpcySqsxJob> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxJob::getFlcmc, lcmc);
        queryWrapper.eq(SpcySqsxJob::getFpid, sqsxid);
        queryWrapper.orderByAsc(SpcySqsxJob::getFfssj);
        queryWrapper.orderByAsc(SpcySqsxJob::getCreateTime);
        List<SpcySqsxJob> jobList = spcySqsxJobService.list(queryWrapper);

        return jobList;
    }

    /**
     * 获取当前流程任务相关信息
     * @param jobid 流程任务id
     */
    public Map getJobConcernByJobId(String jobid){
        Map map = new HashMap();

        // 当前流转记录
        SpcySqsxJob job = spcySqsxJobService.getById(jobid);
        if (job == null){
            log.error("当前流程任务id为【{}】的流转记录不存在",jobid);
            return map;
        }
        String sqsxid = job.getFpid();
        if (StringUtils.isBlank(sqsxid)){
            log.error("当前流程任务id为【{}】的流转记录中的申请事项id为空",jobid);
            return map;
        }

        // 申请事项基本信息
        SpcySqsxBasic sqsxBasic = spcySqsxBaiscService.getById(sqsxid);
        if (sqsxBasic != null){
            boolean isUpdate = false;
            if (sqsxBasic.getFslsj() == null) {
                sqsxBasic.setFslsj(new Date());
                isUpdate = true;
            }

            // 获取申请事项-对应的业务分类
            if (StringUtils.isBlank(sqsxBasic.getFjclx())){
                String jclx = this.getDeptJclxBySxmc(sqsxBasic.getFsxmc(), sqsxBasic.getFywflcode());
                if (StringUtils.isNotBlank(jclx)){
                    sqsxBasic.setFjclx(jclx);
                    isUpdate = true;
                }
            }

            // 计算申请事项-完成时限
            if (sqsxBasic.getFwcsx() == null || sqsxBasic.getFwcsx() == 0){
                int wcsx = flowJobBlsxService.initSqsxBlsxBySqsxId(sqsxBasic);
                if (wcsx != 0) {
                    sqsxBasic.setFwcsx(wcsx);
                    sqsxBasic.setFsysx(String.valueOf(wcsx));
                    isUpdate = true;
                }
            }

            // 计算截止日期
            if (sqsxBasic.getFjzrq() == null) {
                Date jzrq = flowJobBlsxService.jsSqsxJzrq(sqsxBasic);
                if (jzrq != null){
                    sqsxBasic.setFjzrq(jzrq);
                    isUpdate = true;
                }
            }

            if (isUpdate) {
                spcySqsxBaiscService.updateById(sqsxBasic);
            }
        }

        // 企业相关信息
        SpcyQyJbxx qyJbxx = null;
        if (sqsxBasic != null && StringUtils.isNotBlank(sqsxBasic.getFqyid())){
            qyJbxx = spcyQyJbxxService.getById(sqsxBasic.getFqyid());
        }

        // 流程任务流传记录
        List<SpcySqsxJob> jobList = getJobListBySqsxId(sqsxid);

        // 申请事项相关文件记录
        List<SpcySqsxWjjl> wjjlList = new ArrayList<>();

        // 申请事项的选派记录-查验
        List<SpcySqsxXpjl> cyxpjlList = getTyXpXpjlList(sqsxid,SpcyConstant.FLOW_TYPE_CY);

        // 申请事项的选派记录-审评
        List<SpcySqsxXpjl> spxpjlList = getTyXpXpjlList(sqsxid,SpcyConstant.FLOW_TYPE_SP);

        // 获取下环节待办人
        Map nextDbr = this.getNextDbr(sqsxid, jobid);
        String nextNode = nextDbr.get("nextNode").toString();
        List<AgentVo> dbrListByMap = this.getDbrListByMap(nextDbr);

        map.put("sqsx",sqsxBasic); // 申请事项基本信息
        map.put("job",job); // 当前流转记录
        map.put("jobHistoryList",jobList); // 流程任务流传记录
        map.put("wjjlList",wjjlList); // 申请事项-相关文件
        map.put("cyxpjlList",cyxpjlList); // 申请事项-选派记录-查验
        map.put("spxpjlList",spxpjlList); // 申请事项-选派记录-审评
        map.put("nextNodeDbrList",dbrListByMap); // 下环节待办人list
        map.put("nextNode",nextNode);

        map.put("qy",qyJbxx); // 企业基本信息
        return map;
    }

    /**
     * 获取下环节待办人
     * @param sqsxid 申请事项id
     */
    public Map getNextDbr(String sqsxid,String jobid){
        String ywflCode = getYwflCode(sqsxid); // 申请事项分类编码
        String jobNodeName = getJobNodeName(jobid); // 获取当前流程任务job环节

        // 通过业务分类编码，获取对应的FlowConstant
        FlowConstant flowConstant = SpcyConstant.getFlowConstant(ywflCode);

        // 下一个环节名称
        String nextNodeName = "";
        String nodes = "";
        List<String> nodesList = new ArrayList<>();

        if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPSC)){
            nodes = YpscFlowConstant.NODENAME;
            nodesList = Arrays.asList(nodes.split(","));

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPZC)){
            nodes = YpzcFlowConstant.NODENAME;
            nodesList = Arrays.asList(nodes.split(","));

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPJY)){
            nodes = YpltFlowConstant.NODENAME;
            nodesList = Arrays.asList(nodes.split(","));

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YLQX)){
            nodes = YlqxFlowConstant.NODENAME;
            nodesList = Arrays.asList(nodes.split(","));

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_HZP)){
            nodes = HzpFlowConstant.NODENAME;
            nodesList = Arrays.asList(nodes.split(","));

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPZCLC)){
            nodes = YpzclcFlowConstant.NODENAME;
            nodesList = Arrays.asList(nodes.split(","));

        }

        if (nodesList.contains(jobNodeName)){
            nextNodeName = flowConstant.getNextNodeName(jobNodeName);
        }else {
            nextNodeName = flowConstant.getSpNextNodeName(jobNodeName);
        }

        Map nodeDbr = new HashMap();
        if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPSC)) {
            // 药品生产
            nodeDbr = ypscFlowService.getNodeDbr(sqsxid, nextNodeName, "");

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPZC)){
            // 药品注册
            nodeDbr = ypzcFlowService.getNodeDbr(sqsxid, nextNodeName, "");

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPJY)){
            // 药品流通
            nodeDbr = ypltFlowService.getNodeDbr(sqsxid, nextNodeName, "");

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YLQX)){
            // 医疗器械
            nodeDbr = ylqxFlowService.getNodeDbr(sqsxid, nextNodeName, "");

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_HZP)){
            // 化妆品
            nodeDbr = hzpFlowService.getNodeDbr(sqsxid, nextNodeName, "");

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPZCLC)){
            // 药品注册
            nodeDbr = ypzclcFlowService.getNodeDbr(sqsxid, nextNodeName, "");

        }else {
            log.error("不存在该业务分类: {}",ywflCode);
        }

        nodeDbr.put("nextNode",nextNodeName);

        return nodeDbr;
    }

    /**
     * 获取第一个环节待办人
     */
    public Map getFirstNodeDbr(String fywflcode){
        Map map = new HashMap();

        List<AgentVo> dbrListByMap = new ArrayList<>();
        String nextNode = "";

        FlowConstant flowConstant1 = SpcyConstant.getFlowConstant(fywflcode);
        String firstNode = flowConstant1.getFirstNode();
        nextNode = flowConstant1.getNextNodeName(firstNode); // 下环节名称

        if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPSC)) {
            // 药品生产
            Map nodeDbr = ypscFlowService.getNodeDbr("", nextNode, "");
            dbrListByMap = getDbrListByMap(nodeDbr);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPZC)){
            // 药品注册
            Map nodeDbr = ypzcFlowService.getNodeDbr("", nextNode, "");
            dbrListByMap = getDbrListByMap(nodeDbr);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPJY)){
            // 药品流通
            Map nodeDbr = ypltFlowService.getNodeDbr("", nextNode, "");
            dbrListByMap = getDbrListByMap(nodeDbr);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YLQX)){
            // 医疗器械
            Map nodeDbr = ylqxFlowService.getNodeDbr("", nextNode, "");
            dbrListByMap = getDbrListByMap(nodeDbr);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_HZP)){
            // 化妆品
            Map nodeDbr = hzpFlowService.getNodeDbr("", nextNode, "");
            dbrListByMap = getDbrListByMap(nodeDbr);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPZCLC)){
            // 药品注册
            Map nodeDbr = ypzclcFlowService.getNodeDbr("", nextNode, "");
            dbrListByMap = getDbrListByMap(nodeDbr);

        }else {
            log.error("不存在该业务分类: {}",fywflcode);
        }

        map.put("nextNodeDbrList",dbrListByMap); // 下环节待办人list
        map.put("nextNode",nextNode);
        return map;
    }

    /**
     * 获取指定第一个环节环节待办人
     */
    public Map getZdFirstNodeDbr(String fywflcode,String firstNode){
        Map map = new HashMap();

        List<AgentVo> dbrListByMap = new ArrayList<>();
        String nextNode = "";

        FlowConstant flowConstant1 = SpcyConstant.getFlowConstant(fywflcode);
        nextNode = flowConstant1.getNextNodeName(firstNode); // 下环节名称

        if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPSC)) {
            // 药品生产
            Map nodeDbr = ypscFlowService.getNodeDbr("", nextNode, "");
            dbrListByMap = getDbrListByMap(nodeDbr);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPZC)){
            // 药品注册
            Map nodeDbr = ypzcFlowService.getNodeDbr("", nextNode, "");
            dbrListByMap = getDbrListByMap(nodeDbr);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPJY)){
            // 药品流通
            Map nodeDbr = ypltFlowService.getNodeDbr("", nextNode, "");
            dbrListByMap = getDbrListByMap(nodeDbr);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YLQX)){
            // 医疗器械
            Map nodeDbr = ylqxFlowService.getNodeDbr("", nextNode, "");
            dbrListByMap = getDbrListByMap(nodeDbr);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_HZP)){
            // 化妆品
            Map nodeDbr = hzpFlowService.getNodeDbr("", nextNode, "");
            dbrListByMap = getDbrListByMap(nodeDbr);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPZCLC)){
            // 药品注册
            Map nodeDbr = ypzclcFlowService.getNodeDbr("", nextNode, "");
            dbrListByMap = getDbrListByMap(nodeDbr);

        }else {
            log.error("不存在该业务分类: {}",fywflcode);
        }

        map.put("nextNodeDbrList",dbrListByMap); // 下环节待办人list
        map.put("nextNode",nextNode);
        return map;
    }

    /**
     * 获取第一个环节待办人
     */
    public Map getSlhjNodeDbr(String fywflcode){
        Map map = new HashMap();

        List<AgentVo> dbrListByMap = new ArrayList<>();
        String nextNode = "";

        FlowConstant flowConstant1 = SpcyConstant.getFlowConstant(fywflcode);
        String firstNode = flowConstant1.getFirstNode();
        nextNode = firstNode;
        if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPSC)) {
            // 药品生产
            Map nodeDbr = ypscFlowService.getNodeDbr("", nextNode, "");
            dbrListByMap = getDbrListByMap(nodeDbr);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPZC)){
            // 药品注册
            Map nodeDbr = ypzcFlowService.getNodeDbr("", nextNode, "");
            dbrListByMap = getDbrListByMap(nodeDbr);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPJY)){
            // 药品流通
            Map nodeDbr = ypltFlowService.getNodeDbr("", nextNode, "");
            dbrListByMap = getDbrListByMap(nodeDbr);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YLQX)){
            // 医疗器械
            Map nodeDbr = ylqxFlowService.getNodeDbr("", nextNode, "");
            dbrListByMap = getDbrListByMap(nodeDbr);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_HZP)){
            // 化妆品
            Map nodeDbr = hzpFlowService.getNodeDbr("", nextNode, "");
            dbrListByMap = getDbrListByMap(nodeDbr);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPZCLC)){
            // 药品注册
            Map nodeDbr = ypzclcFlowService.getNodeDbr("", nextNode, "");
            dbrListByMap = getDbrListByMap(nodeDbr);

        }else {
            log.error("不存在该业务分类: {}",fywflcode);
        }

        map.put("nextNodeDbrList",dbrListByMap); // 下环节待办人list
        return map;
    }

    /**
     * 将待办人map转为List
     * @param map { dbrId: 待办人id ; dbr: 待办人 }
     */
    public List<AgentVo> getDbrListByMap(Map map){
        String dbrids = map.get("dbrId").toString(); // 待办人id
        String dbrs = map.get("dbr").toString(); // 待办人

        List<AgentVo> agentList = new ArrayList<>();

        if (StringUtils.isNotBlank(dbrids)) {
            String agentIds[] = dbrids.split(",|，");
            String agentNames[] = dbrs.split(",|，");

            for (int i = 0; i < agentIds.length; i++) {
                AgentVo agentVo = new AgentVo();
                agentVo.setDbrid(agentIds[i]);
                agentVo.setDbr(agentNames[i]);
                agentList.add(agentVo);
            }
        }

        return agentList;
    }

    // 现场检查-同意选派人员id
    public String getDbrXcjc(String sqsxid){
        String nextNodeDbrIds = "";
        // 核查员
        List<SpcySqsxXpjl> cyXpList = getTyXpXpjlList(sqsxid, SpcyConstant.FLOW_TYPE_CY);

        if (CollectionUtils.isNotEmpty(cyXpList)) {
            List<String> jcyIdList = cyXpList.stream().map(SpcySqsxXpjl::getFjcyid).collect(Collectors.toList());
            List<SpcyRsHcy> hcyList = spcyRsHcyService.listByIds(jcyIdList);
            List<String> userIdList = hcyList.stream().map(SpcyRsHcy::getFuserid).collect(Collectors.toList());
            String userids = org.apache.commons.lang3.StringUtils.join(userIdList, ",");
            nextNodeDbrIds = userids; // 待办人id
        }
        return nextNodeDbrIds;
    }

    // 技术审评-同意选派人员id
    public String getDbrJssp(String sqsxid){
        String nextNodeDbrIds = "";
        // 核查员
        List<SpcySqsxXpjl> cyXpList = getTyXpXpjlList(sqsxid, SpcyConstant.FLOW_TYPE_SP);

        if (CollectionUtils.isNotEmpty(cyXpList)) {
            List<String> jcyIdList = cyXpList.stream().map(SpcySqsxXpjl::getFjcyid).collect(Collectors.toList());
            List<SpcyRsHcy> hcyList = spcyRsHcyService.listByIds(jcyIdList);
            List<String> userIdList = hcyList.stream().map(SpcyRsHcy::getFuserid).collect(Collectors.toList());
            String userids = org.apache.commons.lang3.StringUtils.join(userIdList, ",");
            nextNodeDbrIds = userids; // 待办人id
        }
        return nextNodeDbrIds;
    }

    // 更新现场检查人员工作状态
    public void updateCyUserGzzt(String sqsxId){
//        List<SpcySqsxXpjl> xpList = getXpjlBySqsxId(sqsxId, "");
        List<SpcySqsxXpjl> xpList = spcySqsxXpjlService.getTyXpXpjlListNotOrder(sqsxId);
        this.updateXpryUserGzztInZg(xpList);

        /*if (CollectionUtils.isNotEmpty(cyXpList)){
            List<String> jcyIdList = cyXpList.stream().map(SpcySqsxXpjl::getFjcyid).collect(Collectors.toList());
            List<SpcyRsHcy> hcyList = spcyRsHcyService.listByIds(jcyIdList);
            List<String> userIdList = hcyList.stream().map(SpcyRsHcy::getFuserid).collect(Collectors.toList());
            List<YcSysUser> userList = ycSysUserService.listByIds(userIdList);
            // 修改核查员的工作状态
            for (YcSysUser user : userList){
                if (user.getFgzzt().contains(SpcyConstant.JCY_ZY_ZXRWZ)) {
                    user.setFgzzt(SpcyConstant.JCY_ZT_ZG); // 在岗
                    ycSysUserService.updateById(user);
                }
            }
        }*/
    }

    // 更新技术审评人员工作状态
    public void updateSpUserGzzt(String sqsxId){
        List<SpcySqsxXpjl> xpList = getXpjlBySqsxId(sqsxId, SpcyConstant.FLOW_TYPE_SP);
        this.updateXpryUserGzztInZg(xpList);

        // 审评完成时间
        LambdaUpdateWrapper<SpcySqsxBasic> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxBasic::getId, sqsxId);

        SpcySqsxBasic updateBean = new SpcySqsxBasic();
        updateBean.setFspwczt("是");

        spcySqsxBaiscService.update(updateBean, updateWrapper);

    }

    // 更新选派人员工作状态-在岗
    public void updateXpryUserGzztInZg(List<SpcySqsxXpjl> xpList){

        if (xpList.size() > 3){
            List<String> idsList = xpList.stream()
                    .map(SpcySqsxXpjl::getFuserid)
                    .filter(f -> StringUtils.isNotBlank(f))
                    .distinct()
                    .collect(Collectors.toList());

            ycSysUserService.updateUserGzztInZg(idsList);
        }else {
            for (int i = 0; i < xpList.size(); i++) {
                ycSysUserService.updateUserGzztInZg(xpList.get(i).getFuserid());
            }
        }
    }

    /**
     *  重新添加流程任务待办人
     */
    public Result againAddFlowJobDbrId(String jobid,String dbrid){
        SpcySqsxJob job = spcySqsxJobService.getById(jobid);
        if (job == null){
            log.error("流程任务不存在！");
            return Result.error("流程任务不存在！");
        }
        String oldDbrId = job.getFdbrid(); // 原来的待办人id
        String oldDbr = job.getFdbr();

        List<String> oldDbrIdList = new ArrayList<>(StringPatternUtils.getListByIds(oldDbrId));
        List<String> oldDbrList = new ArrayList<>(StringPatternUtils.getListByIds(oldDbr));

        List<String> newDbrIdsList = StringPatternUtils.getListByIds(dbrid);

        String log = "修改流程待办人：";
        log += job.getFlcmc() + "->" + job.getFhjmc();
        log += "->原待办人["+job.getFdbr()+"]";

        List<YcSysUser> userList = ycSysUserService.listByIds(newDbrIdsList);
        if (CollectionUtils.isNotEmpty(userList)){
            for (YcSysUser user : userList){
                oldDbrList.add(user.getRealname());
                log += "新待办人[ "+user.getRealname() + " ]";
            }

            oldDbrIdList.addAll(newDbrIdsList);
        }

        job.setFdbrid(String.join(",",oldDbrIdList));
        job.setFdbr(String.join(",",oldDbrList));
        boolean flag = spcySqsxJobService.updateById(job);
        if (flag){
            spcyLogService.addLogs(job.getFhjmc(),log,job.getFpid(),SpcyConstant.LOGS_SYS_TYPE_XG,SpcyConstant.LOGS_SYS_TYPE_BC);
        }

        return Result.ok("操作成功");
    }

}
